转换JODA日期时间



我有一个像 "2007-03-12T04:27:00.000+01:00"这样的字符串,我想将其转换为dateTime对象并进行一些计算。目前,我正在使用joda.time。当我将其转换为Joda DateTime对象并尝试打印为"2007-03-12T08:57:00.000+05:30"。如何使用相同时区打印值。

除非您指定所需的时区,否则dateTime默认为机器本地时区。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);
// 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的详细列表

最新更新