Java - ZonedDateTime 没有正确转换为 Date 对象? [英] Java - ZonedDateTime does not correctly convert to Date object?

查看:64
本文介绍了Java - ZonedDateTime 没有正确转换为 Date 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Java 代码:

I have the following Java code:

Date convertedDate = Date.from(zonedDateTime.toInstant());

我遇到的问题是 convertedDate 与 zonedDateTime 对象相比不正确.

The Issue I am having is that convertedDateis not correct in comparison to the zonedDateTime object.

例如,当我有一个 zonedDateTime 对象时:

For Example when I have a zonedDateTime object of:

2021-09-16T12:00

带区域:

Africa/Abidjan

上面的代码将其转换为:

The code above converts this to:

Thu Sep 16 13:00:00 BST 2021

我在这里期待的是

 Thu Sep 16 10:00:00 BST 2021

由于 Africa/Abidjan 时区比英国夏令时早 2 小时.

As the Africa/Abidjan timezone is 2 hours ahead of BST.

我该如何解决这个问题?

How can I solve this?

推荐答案

java.time

java.util 日期时间 API 及其格式化 API,SimpleDateFormat 已经过时且容易出错.建议完全停止使用它们并切换到 现代日期时间 API*.

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

使用现代日期时间 API java.time 的解决方案:

Solution using java.time, the modern Date-Time API:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        // The given ZonedDateTime
        ZonedDateTime zdtAbidjan = ZonedDateTime.of(
                                        LocalDateTime.of(LocalDate.of(2021, 9, 16),
                                        LocalTime.of(12, 0)),
                                        ZoneId.of("Africa/Abidjan")
                                    );
        System.out.println(zdtAbidjan);

        ZonedDateTime zdtLondon = zdtAbidjan.withZoneSameInstant(ZoneId.of("Europe/London"));
        System.out.println(zdtLondon);
    }
}

输出:

2021-09-16T12:00Z[Africa/Abidjan]
2021-09-16T13:00+01:00[Europe/London]

在线演示

  • 输出中的 Z时区指示符 用于零时区偏移.它代表祖鲁语并指定 Etc/UTC 时区(时区偏移为 +00:00 小时).

  • The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

从输出中可以明显看出,2021-09-16T12:00Z[Africa/Abidjan] 等于 2021-09-16T13:00+01:00[Europe/London].

From the output, it is clear that 2021-09-16T12:00Z[Africa/Abidjan] is equal to 2021-09-16T13:00+01:00[Europe/London].

Trail: Date 了解有关现代日期时间 API 的更多信息时间.

Learn more about the modern Date-Time API from Trail: Date Time.

* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 &7. 如果您正在为 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ API 可通过 desugaring如何在 Android 项目中使用 ThreeTenABP.

* 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 - ZonedDateTime 没有正确转换为 Date 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆