将 java.util.Date 转换为 java.time.LocalDate [英] Convert java.util.Date to java.time.LocalDate

查看:28
本文介绍了将 java.util.Date 转换为 java.time.LocalDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.util.Date 对象转换为新的 JDK 8/JSR-310 java.time.LocalDate 的最佳方法是什么?

What is the best way to convert a java.util.Date object to the new JDK 8/JSR-310 java.time.LocalDate?

Date input = new Date();
LocalDate date = ???

推荐答案

简短回答

Date input = new Date();
LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

说明

尽管名称如此,java.util.Date 表示时间线上的一个瞬间,而不是日期".对象中存储的实际数据是自 1970-01-01T00:00Z(格林威治标准时间 1970/UTC 开始时的午夜)以来的 long 毫秒计数.

Despite its name, java.util.Date represents an instant on the time-line, not a "date". The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z (midnight at the start of 1970 GMT/UTC).

在 JSR-310 中与 java.util.Date 等效的类是 Instant,因此有一个方便的方法 toInstant()提供转换:

The equivalent class to java.util.Date in JSR-310 is Instant, thus there is a convenient method toInstant() to provide the conversion:

Date input = new Date();
Instant instant = input.toInstant();

java.util.Date 实例没有时区的概念.如果您在 java.util.Date 上调用 toString(),这可能看起来很奇怪,因为 toString 是相对于时区的.然而,该方法实际上使用 Java 的默认时区即时提供字符串.时区不是 java.util.Date 的实际状态的一部分.

A java.util.Date instance has no concept of time-zone. This might seem strange if you call toString() on a java.util.Date, because the toString is relative to a time-zone. However that method actually uses Java's default time-zone on the fly to provide the string. The time-zone is not part of the actual state of java.util.Date.

Instant 也不包含任何有关时区的信息.因此,要将 Instant 转换为本地日期,必须指定时区.这可能是默认区域 - ZoneId.systemDefault() - 或者它可能是您的应用程序控制的时区,例如来自用户首选项的时区.使用 atZone() 方法应用时区:

An Instant also does not contain any information about the time-zone. Thus, to convert from an Instant to a local date it is necessary to specify a time-zone. This might be the default zone - ZoneId.systemDefault() - or it might be a time-zone that your application controls, such as a time-zone from user preferences. Use the atZone() method to apply the time-zone:

Date input = new Date();
Instant instant = input.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());

A ZonedDateTime 包含由本地日期和时间、时区和 GMT/UTC 偏移量组成的状态.因此日期 - LocalDate - 可以使用 toLocalDate() 轻松提取:

A ZonedDateTime contains state consisting of the local date and time, time-zone and the offset from GMT/UTC. As such the date - LocalDate - can be easily extracted using toLocalDate():

Date input = new Date();
Instant instant = input.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
LocalDate date = zdt.toLocalDate();

Java 9 答案

在 Java SE 9 中,新方法已添加,稍微简化了此任务:

In Java SE 9, a new method has been added that slightly simplifies this task:

Date input = new Date();
LocalDate date = LocalDate.ofInstant(input.toInstant(), ZoneId.systemDefault());

这种新的替代方案更直接,产生的垃圾更少,因此性能应该更好.

This new alternative is more direct, creating less garbage, and thus should perform better.

这篇关于将 java.util.Date 转换为 java.time.LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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