2012-10-19 33 views
1

我有一个像"2007-03-12T04:27:00.000+01:00"这样的字符串,我想将它转换为datetime对象并做一些计算。目前我正在使用joda.time。当我将其转换为joda DateTime对象并尝试打印时,它显示为"2007-03-12T08:57:00.000+05:30"。我怎样才能打印与同一时区的价值。转换约定日期时间

+2

时区是不同的值是正确的 –

回答

0

的详细列表,除非你指定你想要的时区,日期默认为计算机本地时区。 DateTime(Object)构造函数使用偏移量来确定您的意思是什么时刻,但它不使用偏移量来设置时区。所以而不是:

DateTime localDateTime = new DateTime("2007-03-12T04:27:00.000+01:00"); 

解析偏移量并获取适当的DateTimeZone并将其赋予DateTime。

DateTimeZone zone1 = DateTimeZone.forOffsetHoursMinutes(01, 00); 
    DateTime localDateTime = new DateTime("2007-03-12T04:27:00.000", zone1); 
0
// get current moment in default time zone 
DateTime dt = new DateTime(); 
// translate to London local time 
DateTime dtLondon = dt.withZone(DateTimeZone.forID("Time Zone ID here"));//Europe/Paris 

你可以找到时区ID here

+0

这里的时区是不固定的。它可以改变。所以我不能在代码中对TimeZone ID进行硬编码。有什么方法可以从日期字符串中读取时区ID(末尾有+01:00) – Vishnu