簡體   English   中英

如何使用 ZonedDateTime 或 Java 8 將任何日期時間轉換為 UTC

[英]How to convert any Date time to UTC using ZonedDateTime or Java 8

我正在嘗試使用ZonedDateTime將日期06-12-2015 02:10:10 PM從默認區域轉換為 UTC。

LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
ZonedDateTime utc = ZonedDateTime.of(localDateTime, ZoneOffset.UTC);

utc返回2015-12-06T14:10:10Z而不是06-12-2015 09:10:10 AM

如何將日期從默認區域轉換為 UTC? 此處給出的答案將當前時間轉換為 UTC。

您可以使用ZonedDateTime.ofInstant(Instant, ZoneId)其中第二個參數是UTC (即時知道本地偏移量)。 就像是,

String source = "06-12-2015 02:10:10 PM";
String pattern = "MM-dd-yyyy hh:mm:ss a";
DateFormat sdf = new SimpleDateFormat(pattern);
try {
    Date date = sdf.parse(source);
    ZonedDateTime zdt = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC"));
    System.out.println(zdt.format(DateTimeFormatter.ofPattern(pattern)));
} catch (ParseException e) {
    e.printStackTrace();
}

我得到(對應於我的本地區域偏移量)

06-12-2015 06:10:10 PM

06-12-2015 02:10:10 PM 在巴基斯坦 = 06-12-2015 09:10:10 AM(UTC)

有很多方法可以做到。

  1. 解析為LocalDateTime ➡️ 將其與您的時區結合以獲得ZonedDateTime ➡️ 轉換為Instant ➡️ 使用Instant#atZone和 UTC 時區轉換為ZonedDateTime
import java.time.Instant;
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) {
        String strDateTime = "06-12-2015 02:10:10 PM";

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);

        LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);

        // Using ZoneId.of("Asia/Karachi") for the demo. Change it to
        // ZoneId.systemDefault()
        Instant instant = ldt.atZone(ZoneId.of("Asia/Karachi")).toInstant();

        ZonedDateTime zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));

        System.out.println(zdtUtc.format(dtf)); // 06-12-2015 09:10:10 AM
    }
}
  1. 解析為LocalDateTime ➡️ 將其與您的時區結合以獲得ZonedDateTime ➡️ 轉換為Instant ➡️ 使用ZonedDateTime#ofInstant和 UTC 時區轉換為ZonedDateTime
import java.time.Instant;
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) {
        String strDateTime = "06-12-2015 02:10:10 PM";

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);

        LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);

        // Using ZoneId.of("Asia/Karachi") for the demo. Change it to
        // ZoneId.systemDefault()
        Instant instant = ldt.atZone(ZoneId.of("Asia/Karachi")).toInstant();

        ZonedDateTime zdtUtc = ZonedDateTime.ofInstant(instant, ZoneId.of("Etc/UTC"));

        System.out.println(zdtUtc.format(dtf)); // 06-12-2015 09:10:10 AM
    }
}
  1. 使用ZonedDateTime#withZoneSameInstant
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) {
        String strDateTime = "06-12-2015 02:10:10 PM";

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);

        LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);

        // Using ZoneId.of("Asia/Karachi") for the demo. Change it to
        // ZoneId.systemDefault()
        ZonedDateTime zdtPak = ldt.atZone(ZoneId.of("Asia/Karachi"));

        ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));

        System.out.println(zdtUtc.format(dtf)); // 06-12-2015 09:10:10 AM
    }
}
  1. 使用DateTimeFormatter#withZoneZonedDateTime#withZoneSameInstant
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) {
        String strDateTime = "06-12-2015 02:10:10 PM";

        // Using ZoneId.of("Asia/Karachi") for the demo. Change it to
        // ZoneId.systemDefault()
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("M-d-u h:m:s a", Locale.ENGLISH)
                                        .withZone(ZoneId.of("Asia/Karachi"));

        ZonedDateTime zdtPak = ZonedDateTime.parse(strDateTime, dtfInput);

        ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);
        System.out.println(zdtUtc.format(dtfOutput)); // 06-12-2015 09:10:10 AM
    }
}

Trail: Date Time 中了解有關現代日期時間 API * 的更多信息。


* 出於任何原因,如果您必須堅持使用 Java 6 或 Java 7,您可以使用ThreeTen-Backport,它將大部分java.time功能向后移植到 Java 6 和 7。如果您正在為 Android 項目和您的 Android API 工作級別仍然不符合 Java-8,請檢查 通過 desugaringHow to use ThreeTenABP in Android Project 可用的 Java 8+ APIs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM