将Joda-Time`DateTime`与时区转换为DateTime而没有时区? [英] Convert Joda-Time `DateTime` with timezone to DateTime without timezone?
问题描述
鉴于 DateTime
,例如 2015-07-09T05:10:00 + 02:00
使用 Joda-Time ?
如何将其转换为本地时间,这意味着添加时区本身。
所需输出: 2015-07-09T07:10:00
How can I convert it to local time, meaning adding the timezone to the time itself.
Desired output: 2015-07-09T07:10:00
我尝试过 dateTime.toDateTime(DateTimeZone.UTC)
,但没有给出所需的结果。
I tried dateTime.toDateTime(DateTimeZone.UTC)
but that did not give the desired result.
推荐答案
@Nazgul说的是对的,但是如果你想要实现的是UTC区域的墙上时间,你可以这样做:
What @Nazgul said is right, but in case all you want to achieve is a "wall-time" in UTC zone you can do something like that:
DateTime dateTimePlus2 = DateTime.parse("2015-07-09T05:10:00+02:00");
System.out.println(dateTimePlus2);
DateTime dateTimeUTC = dateTimePlus2.withZone(DateTimeZone.UTC);
System.out.println(dateTimeUTC);
LocalDateTime localDateTimeUTC = dateTimeUTC.toLocalDateTime();
System.out.println(localDateTimeUTC);
结果:
2015-07-09T05:10:00.000+02:00
2015-07-09T03:10:00.000Z ("Z" == Zulu tz == UTC)
2015-07-09T03:10:00.000
正如你所看到的,时间不是07:10正如你所料,因为UTC + 2区域是UTC的两小时前进。转换为UTC 减去 2小时。
As you can see, the time is not "07:10" as you expected, because UTC+2 zone is two hours ahead of UTC. Converting to UTC subtracts 2 hours.
这篇关于将Joda-Time`DateTime`与时区转换为DateTime而没有时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!