如何使用 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 returns 2015-12-06T14:10:10Z 而不是 06-12-2015 09:10:10 AM

如何将日期从默认时区转换为 UTC?给出的答案 here 将当前时间转换为 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 巴基斯坦下午 = 06-12-2015 09:10:10 上午 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
    }
}

了解有关 modern 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