Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java.time根据时区转换时间_Java_Time_Java 8_Java Time - Fatal编程技术网

使用java.time根据时区转换时间

使用java.time根据时区转换时间,java,time,java-8,java-time,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中基于时区更改时间,我在这里构建了一个时区为EST的日期,现在我需要找到相应时间的UTC。请帮我解决这个问题

String str = "16Jun2015_153556";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMMyyyy_HHmmss");
formatter.withZone(ZoneId.of("EST5EDT"));
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
您不应该考虑更改
LocalDateTime
的“时区”——
LocalDateTime
没有时区。相反,您应该从
LocalDateTime
和时区(
ZoneId
)构建一个
ZonedDateTime
)。首先删除格式化程序。withZone调用,然后使用:

ZonedId zone = ZoneId.of("EST5EDT"); // Or preferrably "America/New_York"
ZonedDateTime zoned = ZonedDateTime.of(dateTime, zone);
然后您可以将其转换为瞬间,或者使用:

ZonedDateTime utc = zoned.withZoneSameInstant(ZoneOffset.UTC);
例如:

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

这个答案可能比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
然后将本地日期时间指定给EST区域。使用IANA符号“America/New_York”比使用过时的形式“EST5EDT”更安全(EST5EDT仅支持固定dst规则,没有任何历史原始偏移量)

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

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

表达式
formatter.withZone(ZoneId.of(“EST5EDT”)
是无用的,因为您没有将它分配给
DateTimeFormatter
的任何实例。请记住,该类是不可变的,因此所有更改只能通过复制而不是操作同一实例来实现。以这种方式设置时区不会影响对
LocalDateTime
的解析(区域将被忽略)。
LocalDateTime utc = zdt.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime();
System.out.println(utc); // 2015-06-16T19:35:56