[英]Convert time based on timezone using java.time
How to change the time based on timezone in LocalDateTime , here i have built a date with Time zone as EST , now i need to find the UTC of the corresponding time. 如何在LocalDateTime中基于时区更改时间,在这里我建立了一个时区为EST的日期,现在我需要找到相应时间的UTC 。 please help me how to solve this 请帮我解决这个问题
String str = "16Jun2015_153556";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMMyyyy_HHmmss");
formatter.withZone(ZoneId.of("EST5EDT"));
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
You shouldn't think about "changing the time zone" of a LocalDateTime
- a LocalDateTime
doesn't have a time zone. 你不应该去想“更改时区”一户LocalDateTime
-一个LocalDateTime
没有一个时区。 Instead, you should build a ZonedDateTime
from a LocalDateTime
and a time zone ( ZoneId
). 相反,你应该建立一个ZonedDateTime
从LocalDateTime
和时区( ZoneId
)。 First remove the formatter.withZone
call, then use: 首先删除formatter.withZone
调用,然后使用:
ZonedId zone = ZoneId.of("EST5EDT"); // Or preferrably "America/New_York"
ZonedDateTime zoned = ZonedDateTime.of(dateTime, zone);
Then you could convert that to an instant, or perhaps use: 然后,您可以将其转换为即时信息,或者使用:
ZonedDateTime utc = zoned.withZoneSameInstant(ZoneOffset.UTC);
So for example: 因此,例如:
import java.time.*;
import java.time.format.*;
public class Test {
public static void main(String[] args) {
String str = "16Jun2015_153556";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMMyyyy_HHmmss");
ZoneId zone = ZoneId.of("America/New_York");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
ZonedDateTime zoned = ZonedDateTime.of(dateTime, zone);
// Both of these print 2015-06-16T19:35:56Z
System.out.println(zoned.toInstant());
System.out.println(zoned.withZoneSameInstant(ZoneOffset.UTC));
}
}
This answer might be somehow more structured than the correct answer of Jon Skeet. 这个答案可能比Jon Skeet的正确答案更加结构化。 In my comment above I have also pointed out not to overlook the immutable nature of DateTimeFormatter
so please always assign the result of any method prefixed by "with...()" to a variable of same type. 在上面的评论中,我还指出了不要忽视DateTimeFormatter
的不变性质,因此请始终将以“ with ...()”为前缀的任何方法的结果分配给相同类型的变量。
// parsing your string input
// NO!!! timezone is needed in this step because LocalDateTime is just without timezone
String str = "16Jun2015_153556";
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("ddMMMuuuu_HHmmss", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(str, formatter);
System.out.println(ldt); // your input as java.time-object: 2015-06-16T15:35:56
Then you assign your local date-time to the EST-zone. 然后,您将本地日期时间分配给EST区。 It is safer to use the IANA-notation "America/New_York" than to use the outdated form "EST5EDT" (which is only supporting fixed dst rules without any history of historical raw offsets). 与使用过时的格式“ EST5EDT”(仅支持固定的dst规则,没有任何历史原始偏移量的历史记录)相比,使用IANA标记“ America / New_York”更为安全。
ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/New_York"));
Finally you transform the intermediate global timestamp back to a local date-time at the offset UTC+00 preserving the same physical time: 最后,您将中间全局时间戳转换为偏移UTC + 00的本地日期时间,并保留相同的物理时间:
LocalDateTime utc = zdt.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime();
System.out.println(utc); // 2015-06-16T19:35:56
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.