繁体   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