使用java.time基于时区转换时间

Aru*_*ore 7 java time java-8 java-time

如何根据LocalDateTime中的时区更改时间,这里我建立了一个带有时区的日期作为EST,现在我需要找到相应时间的UTC.请帮我解决这个问题

String str = "16Jun2015_153556";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMMyyyy_HHmmss");
formatter.withZone(ZoneId.of("EST5EDT"));
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 12

你不应该去想"更改时区"一的LocalDateTime-一个LocalDateTime具有一个时区.相反,您应该ZonedDateTime从a LocalDateTime和时区(ZoneId)构建一个.首先删除formatter.withZone呼叫,然后使用:

ZonedId zone = ZoneId.of("EST5EDT"); // Or preferrably "America/New_York"
ZonedDateTime zoned = ZonedDateTime.of(dateTime, zone);
Run Code Online (Sandbox Code Playgroud)

然后你可以将它转换为瞬间,或者可能使用:

ZonedDateTime utc = zoned.withZoneSameInstant(ZoneOffset.UTC);
Run Code Online (Sandbox Code Playgroud)

例如:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)


Men*_*ild 8

这个答案可能比Jon Skeet的正确答案更具结构性.在上面的评论中,我也指出不要忽视不可变性,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
Run Code Online (Sandbox Code Playgroud)

然后将本地日期时间分配给EST区域.使用IANA符号"America/New_York"比使用过时的"EST5EDT"(仅支持固定dst规则而没有任何历史原始偏移历史)更安全.

ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/New_York"));
Run Code Online (Sandbox Code Playgroud)

最后,将中间全局时间戳转换回偏移UTC + 00的本地日期时间,保留相同的物理时间:

LocalDateTime utc = zdt.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime();
System.out.println(utc); // 2015-06-16T19:35:56
Run Code Online (Sandbox Code Playgroud)