在Joda中将UTC转换为LocalDateTime? [英] Convert UTC to LocalDateTime in Joda?
问题描述
DateTime dt = new DateTime("2014-09-15T21:20:14");
System.out.println(dt);
System.out.println(dt.plusMillis(581042272).toDateTime().toLocalDateTime().toDateTime(DateTimeZone.forID("GMT")));
以dt表示的时间是UTC,我想将以dt表示的时间加上毫秒设置为GMT吗?但是,时间仍显示为UTC(格林尼治标准时间后1小时).如何设置它,使其提前一小时?
the time in dt is in UTC, I want to set the time in dt plus milliseconds to GMT? However, the time is still printed as UTC (1 hour behind GMT). How can I set it so it's one hour in front?
2014-09-15T21:20:14.000+01:00
2014-09-22T14:44:16.272Z
我知道时间恰好晚了一个小时,因为我是在格林尼治标准时间15:44:16发出此请求的.
I know the time is exactly one hour behind because I made this request at 15:44:16 GMT
推荐答案
您的DateTime
实际上不是UTC中的 -它处于系统默认时区.要解决此问题,您只需要告诉它您要传入的值就是UTC:
Your DateTime
is actually not in UTC - it's in the system default time zone. To fix it, you just need to tell it that the value you're passing in is in UTC:
DateTime dt = new DateTime("2014-09-15T21:20:14", DateTimeZone.UTC);
System.out.println(dt);
DateTime other = dt.plusMillis(581042272);
System.out.println(other);
输出:
2014-09-15T21:20:14.000Z
2014-09-22T14:44:16.272Z
还要注意,您尚未在格林尼治标准时间15:44:16发出请求,因为尚未发生.在我撰写本文时,该时间是英国夏令时16:05,因此格林尼治标准时间15:05.重要的是要了解英国的时区不是"GMT",这只是我们不遵守夏令时的时区的一部分.
Also note that you can't have made the request at 15:44:16 GMT, as that hasn't occurred yet. At the time I'm writing this, it's 16:05 British Summer Time, therefore 15:05 GMT. It's important to understand that the time zone in the UK isn't "GMT" - that's just the part of the time zone when we're not observing daylight savings.
如果要转换为英国时区,则需要:
If you want to convert to the UK time zone, you want:
DateTime other = dt.plusMillis(581042272)
.withZone(DateTimeZone.forID("Europe/London"));
这篇关于在Joda中将UTC转换为LocalDateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!