简体   繁体   English

如何使用 ZonedDateTime 或 Java 8 将任何日期时间转换为 UTC

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

I am trying to convert date 06-12-2015 02:10:10 PM from default zone to UTC using ZonedDateTime .我正在尝试使用ZonedDateTime将日期06-12-2015 02:10:10 PM从默认区域转换为 UTC。

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

but utc returns 2015-12-06T14:10:10Z instead of 06-12-2015 09:10:10 AMutc返回2015-12-06T14:10:10Z而不是06-12-2015 09:10:10 AM

How can I convert date from default zone to UTC?如何将日期从默认区域转换为 UTC? The answer given here convert current time to UTC. 此处给出的答案将当前时间转换为 UTC。

You can use ZonedDateTime.ofInstant(Instant, ZoneId) where the second parameter is UTC (the instant knows the local offset).您可以使用ZonedDateTime.ofInstant(Instant, ZoneId)其中第二个参数是UTC (即时知道本地偏移量)。 Something like,就像是,

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

And I get (corresponding to my local zone offset)我得到(对应于我的本地区域偏移量)

06-12-2015 06:10:10 PM

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

There are many ways to do it.有很多方法可以做到。

  1. Parse to LocalDateTime ➡️ Combine it with your timezone to get ZonedDateTime ➡️ Convert to Instant ➡️ Convert to ZonedDateTime using Instant#atZone and UTC timezone.解析为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. Parse to LocalDateTime ➡️ Combine it with your timezone to get ZonedDateTime ➡️ Convert to Instant ➡️ Convert to ZonedDateTime using ZonedDateTime#ofInstant and UTC timezone.解析为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. Using ZonedDateTime#withZoneSameInstant :使用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. Using DateTimeFormatter#withZone and ZonedDateTime#withZoneSameInstant :使用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
    }
}

Learn more about the modern date-time API * from Trail: Date Time .Trail: Date Time 中了解有关现代日期时间 API * 的更多信息。


* For any reason, if you have to stick to Java 6 or Java 7, you can use 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 How to use ThreeTenABP in Android Project . * 出于任何原因,如果您必须坚持使用 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