java 在 Joda 中将 UTC 转换为 LocalDateTime?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25977197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:04:53  来源:igfitidea点击:

Convert UTC to LocalDateTime in Joda?

javatimelocaljodatime

提问by Adz

        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")));

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?

dt 中的时间是 UTC,我想将 dt 中的时间加毫秒设置为 GMT?但是,时间仍打印为 UTC(格林威治标准时间后 1 小时)。我怎样才能把它设置成提前一小时?

2014-09-15T21:20:14.000+01:00
2014-09-22T14:44:16.272Z

I know the time is exactly one hour behind because I made this request at 15:44:16 GMT

我知道时间正好晚了一小时,因为我在格林威治标准时间 15:44:16 提出了这个请求

回答by CularBytes

For those that have trouble with converting datetime from a server to local datetime:

对于那些无法将日期时间从服务器转换为本地日期时间的人:

1.Make sure the server gives you a UTC time, meaning, the format should contain a timezone. 2.Convert with pattern, if the api does not give you an timezone, then you might get an exception because of the last 'Z'.

1.确保服务器为您提供UTC时间,这意味着格式应包含时区。2.用模式转换,如果api没有给你一个时区,那么你可能会因为最后一个'Z'而得到一个异常。

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        DateTime dt = formatter.parseDateTime(currentPost.postDate);

3.to check the time offset (optional)

3.检查时间偏移(可选)

DateTimeZone ActualZone = dt.getZone();

4.Convert to local time

4.转换为当地时间

TimeZone tz2 = TimeZone.getDefault();
        DateTime localdt = new DateTime(dt, DateTimeZone.forID(tz2.getID()));

(if you control the API yourself, and it happens to be an asp.net api, check this, to set the Kindof the datetime, even though you might have saved it as UTC time in the database, you will send the datetime with the default server timezone)

(如果您自己控制 API,并且它恰好是一个 asp.net api,请检查此设置以设置Kind日期时间,即使您可能已将其保存为数据库中的 UTC 时间,您将发送带有默认服务器时区)

回答by Jon Skeet

Your DateTimeis actually notin 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实际上不在UTC 中 - 它在系统默认时区中。要修复它,您只需要告诉它您传入的值是 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);

Output:

输出:

2014-09-15T21:20:14.000Z
2014-09-22T14:44:16.272Z

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.

另请注意,您不能在格林威治标准时间 15:44:16 提出请求,因为这还没有发生。在我写这篇文章的时候,是英国夏令时间 16:05,因此是格林威治标准时间 15:05。重要的是要了解英国的时区不是“GMT”——这只是我们不遵守夏令时时区的一部分。

If you want to convert to the UK time zone, you want:

如果您想转换为英国时区,您需要:

DateTime other = dt.plusMillis(581042272)
    .withZone(DateTimeZone.forID("Europe/London"));

回答by Ferrakkem Bhuiyan

val marketCentreTime = timeInAnotherTimezone.withZone(DateTimeZone.forID("yourCountryName/andyourCityName"));