将日期从 GMT 时区转换为本地时区——使用 ISO_OFFSET_DATE_TIME

Convert date from GMT timezone to local time zone -- using ISO_OFFSET_DATE_TIME

我有一个日期,假设是格林威治标准时间,我想使用 ISO_OFFSET_DATE_TIME 格式将其转换为本地时区。

基本上,我想从:

2018-03-13 03:00:00.0

至:

2018-03-13T00:00:00-09:00

显然这会发生变化,具体取决于您当地的时区。

关于如何做到这一点有什么想法吗?

您可以为此利用 ZonedDateTime。您只需要读入 UTC 日期并根据需要进行转换。你可能会得到这样的东西:

String readPattern = "yyyy-MM-dd HH:mm:ss.S";
DateTimeFormatter readDateTimeFormatter = DateTimeFormatter.ofPattern(readPattern).withZone(ZoneOffset.UTC);
LocalDateTime utcLocalDateTime = LocalDateTime.parse("2018-03-13 03:00:00.0", readDateTimeFormatter);
ZonedDateTime localZonedDateTime = utcLocalDateTime.atOffset(ZoneOffset.UTC).atZoneSameInstant(ZoneId.systemDefault());
String writePattern = "yyyy-MM-dd HH:mm:ssXXX";
DateTimeFormatter writeDateTimeFormatter = DateTimeFormatter.ofPattern(writePattern);
System.out.println(writeDateTimeFormatter.format(localZonedDateTime));

有关详细信息,请参阅:

将日期时间字符串解析为LocalDateTime:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s.S", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse("2018-03-13 03:00:00.0", dtf);

将此与 UTC 偏移相结合以创建 OffsetDateTime:

OffsetDateTime odtUtc = ldt.atOffset(ZoneOffset.UTC);

创建其偏移量设置为 -09:00 的副本,同时保持即时相同:

OffsetDateTime odtUtcMinus9 = odtUtc.withOffsetSameInstant(ZoneOffset.of("+09:00"));

演示:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s.S", Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse("2018-03-13 03:00:00.0", dtf);
        System.out.println(ldt); // 2018-03-13T03:00

        OffsetDateTime odtUtc = ldt.atOffset(ZoneOffset.UTC);
        System.out.println(odtUtc); // 2018-03-13T03:00Z

        OffsetDateTime odtUtcMinus9 = odtUtc.withOffsetSameInstant(ZoneOffset.of("+09:00"));
        System.out.println(odtUtcMinus9); // 2018-03-13T12:00+09:00
    }
}

请注意,时区偏移量是固定的,即它独立于 DST. If you are looking for an automatic adjustment of timezone offset as per the DST, use ZonedDateTime。这些方法与我们在上一个演示中使用的方法非常相似。

演示:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s.S", Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse("2018-03-13 03:00:00.0", dtf);
        System.out.println(ldt); // 2018-03-13T03:00

        ZonedDateTime zdtUtc = ldt.atZone(ZoneId.of("Etc/UTC"));
        System.out.println(zdtUtc); // 2018-03-13T03:00Z[Etc/UTC]

        ZonedDateTime zdtAmericaAdak = zdtUtc.withZoneSameInstant(ZoneId.of("America/Adak"));
        System.out.println(zdtAmericaAdak); // 2018-03-12T18:00-09:00[America/Adak]

        // A custom format
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSXXX", Locale.ENGLISH);
        String formatted = dtfOutput.format(zdtAmericaAdak);
        System.out.println(formatted); // 2018-03-12 18:00:00.000-09:00
    }
}   

详细了解 java.timemodern date-time API* from Trail: Date Time


* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and