将java.util.Date转换为java.time.LocalDate
本文翻译自:Convert java.util.Date to 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
? 将java.util.Date
对象转换为新的JDK 8 / JSR-310 java.time.LocalDate
的最佳方法是什么?
Date input = new Date();
LocalDate date = ???
#1楼
参考:https://stackoom.com/question/1R82g/将java-util-Date转换为java-time-LocalDate
#2楼
Short answer 简短答案
Date input = new Date();
LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Explanation 说明
Despite its name, java.util.Date
represents an instant on the time-line, not a "date". 尽管其名称, java.util.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). 自1970-01-01T00:00Z(1970 GMT / UTC开始的午夜)以来,存储在对象中的实际数据是很long
的毫秒数。
The equivalent class to java.util.Date
in JSR-310 is Instant
, thus there is a convenient method toInstant()
to provide the conversion: JSR-310中与java.util.Date
等效的类是Instant
,因此有一个方便的方法toInstant()
提供转换:
Date input = new Date();
Instant instant = input.toInstant();
A java.util.Date
instance has no concept of time-zone. 一个java.util.Date
实例没有时区的概念。 This might seem strange if you call toString()
on a java.util.Date
, because the toString
is relative to a time-zone. 如果在java.util.Date
上调用toString()
,这似乎很奇怪,因为toString
是相对于时区的。 However that method actually uses Java's default time-zone on the fly to provide the string. 但是,该方法实际上在运行时使用了Java的默认时区来提供字符串。 The time-zone is not part of the actual state of java.util.Date
. 时区不是java.util.Date
实际状态的一部分。
An Instant
also does not contain any information about the time-zone. Instant
也不包含有关时区的任何信息。 Thus, to convert from an Instant
to a local date it is necessary to specify a time-zone. 因此,要将Instant
日期转换为本地日期,必须指定一个时区。 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. 这可能是默认区域ZoneId.systemDefault()
-或可能是您的应用程序控制的时区,例如用户首选项中的时区。 Use the atZone()
method to apply the time-zone: 使用atZone()
方法应用时区:
Date input = new Date();
Instant instant = input.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
A ZonedDateTime
contains state consisting of the local date and time, time-zone and the offset from GMT/UTC. ZonedDateTime
包含由本地日期和时间,时区和距GMT / UTC的偏移量组成的状态。 As such the date - LocalDate
- can be easily extracted using toLocalDate()
: 作为这样的时间- LocalDate
-可以使用容易地提取toLocalDate()
Date input = new Date();
Instant instant = input.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
LocalDate date = zdt.toLocalDate();
Java 9 answer Java 9答案
In Java SE 9, a new method has been added that slightly simplifies this task: 在Java SE 9中,添加了一个新方法 ,该方法稍微简化了此任务:
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. 这个新的替代方法更直接,产生更少的垃圾,因此应该表现得更好。
#3楼
If you're using Java 8, @JodaStephen's answer is obviously the best. 如果您使用的是Java 8,@ JodaStephen的答案显然是最好的。 However, if you're working with the JSR-310 backport , you unfortunately have to do something like this: 但是,如果您使用的是JSR-310反向端口 ,那么很遗憾,您必须执行以下操作:
Date input = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(input);
LocalDate date = LocalDate.of(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH) + 1,cal.get(Calendar.DAY_OF_MONTH));
#4楼
Better way is: 更好的方法是:
Date date = ...;
Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate()
Advantages of this version: 此版本的优点:
works regardless the input is an instance of
java.util.Date
or it's subclassjava.sql.Date
(unlike @JodaStephen's way). 无论输入是java.util.Date
的实例还是其子类java.sql.Date
(与@JodaStephen的方式不同),它都可以工作。 This is common with JDBC originated data. 这对于JDBC原始数据很常见。java.sql.Date.toInstant()
always throws an exception.java.sql.Date.toInstant()
总是抛出异常。it's the same for JDK8 and JDK7 with JSR-310 backport 具有JSR-310反向端口的JDK8和JDK7相同
I personally use an utility class (but this is not backport-compatible): 我个人使用实用程序类(但这与backport不兼容):
/*** Utilities for conversion between the old and new JDK date types * (between {@code java.util.Date} and {@code java.time.*}).* * <p>* All methods are null-safe.*/
public class DateConvertUtils {/*** Calls {@link #asLocalDate(Date, ZoneId)} with the system default time zone.*/public static LocalDate asLocalDate(java.util.Date date) {return asLocalDate(date, ZoneId.systemDefault());}/*** Creates {@link LocalDate} from {@code java.util.Date} or it's subclasses. Null-safe.*/public static LocalDate asLocalDate(java.util.Date date, ZoneId zone) {if (date == null)return null;if (date instanceof java.sql.Date)return ((java.sql.Date) date).toLocalDate();elsereturn Instant.ofEpochMilli(date.getTime()).atZone(zone).toLocalDate();}/*** Calls {@link #asLocalDateTime(Date, ZoneId)} with the system default time zone.*/public static LocalDateTime asLocalDateTime(java.util.Date date) {return asLocalDateTime(date, ZoneId.systemDefault());}/*** Creates {@link LocalDateTime} from {@code java.util.Date} or it's subclasses. Null-safe.*/public static LocalDateTime asLocalDateTime(java.util.Date date, ZoneId zone) {if (date == null)return null;if (date instanceof java.sql.Timestamp)return ((java.sql.Timestamp) date).toLocalDateTime();elsereturn Instant.ofEpochMilli(date.getTime()).atZone(zone).toLocalDateTime();}/*** Calls {@link #asUtilDate(Object, ZoneId)} with the system default time zone.*/public static java.util.Date asUtilDate(Object date) {return asUtilDate(date, ZoneId.systemDefault());}/*** Creates a {@link java.util.Date} from various date objects. Is null-safe. Currently supports:<ul>* <li>{@link java.util.Date}* <li>{@link java.sql.Date}* <li>{@link java.sql.Timestamp}* <li>{@link java.time.LocalDate}* <li>{@link java.time.LocalDateTime}* <li>{@link java.time.ZonedDateTime}* <li>{@link java.time.Instant}* </ul>* * @param zone Time zone, used only if the input object is LocalDate or LocalDateTime.* * @return {@link java.util.Date} (exactly this class, not a subclass, such as java.sql.Date)*/public static java.util.Date asUtilDate(Object date, ZoneId zone) {if (date == null)return null;if (date instanceof java.sql.Date || date instanceof java.sql.Timestamp)return new java.util.Date(((java.util.Date) date).getTime());if (date instanceof java.util.Date)return (java.util.Date) date;if (date instanceof LocalDate)return java.util.Date.from(((LocalDate) date).atStartOfDay(zone).toInstant());if (date instanceof LocalDateTime)return java.util.Date.from(((LocalDateTime) date).atZone(zone).toInstant());if (date instanceof ZonedDateTime)return java.util.Date.from(((ZonedDateTime) date).toInstant());if (date instanceof Instant)return java.util.Date.from((Instant) date);throw new UnsupportedOperationException("Don't know hot to convert " + date.getClass().getName() + " to java.util.Date");}/*** Creates an {@link Instant} from {@code java.util.Date} or it's subclasses. Null-safe.*/public static Instant asInstant(Date date) {if (date == null)return null;elsereturn Instant.ofEpochMilli(date.getTime());}/*** Calls {@link #asZonedDateTime(Date, ZoneId)} with the system default time zone.*/public static ZonedDateTime asZonedDateTime(Date date) {return asZonedDateTime(date, ZoneId.systemDefault());}/*** Creates {@link ZonedDateTime} from {@code java.util.Date} or it's subclasses. Null-safe.*/public static ZonedDateTime asZonedDateTime(Date date, ZoneId zone) {if (date == null)return null;elsereturn asInstant(date).atZone(zone);}}
The asLocalDate()
method here is null-safe, uses toLocalDate()
, if input is java.sql.Date
(it may be overriden by the JDBC driver to avoid timezone problems or unnecessary calculations), otherwise uses the abovementioned method. 如果输入为java.sql.Date
(如果为避免时区问题或不必要的计算而被JDBC驱动程序覆盖toLocalDate()
,则此处的asLocalDate()
方法为null安全,使用toLocalDate()
),否则使用上述方法。
#5楼
LocalDate ld = new java.sql.Date( new java.util.Date().getTime() ).toLocalDate();
#6楼
LocalDate localDate = LocalDate.parse( new SimpleDateFormat("yyyy-MM-dd").format(date) );
将java.util.Date转换为java.time.LocalDate相关推荐
- 如何将java.util.Date转换为java.sql.Date?
我试图使用java.util.Date作为输入,然后用它创建一个查询-所以我需要一个java.sql.Date . 我很惊讶地发现它不能隐式或显式地进行转换-但我什至不知道该怎么做,因为Java AP ...
- java8 util.time_Java8 java.util.Date转换为java.time.ZonedDateTime
尝试将java.util.Date转换为java.time.LocalDate时,我收到以下异常. java.time.DateTimeException: Unable to obtain Zone ...
- localBlock在java_将java.util.Date转换为java.time.LocalDate
小编典典 Date input = new Date(); LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLo ...
- java.util.Date和java.sql.Date之间的相互转换
1.java.util.Date转换为java.sql.Date java.util.Date curDate = new java.util.Date(); java.sql.Date date = ...
- java.util.Date和java.sql.Date
java.util.Date是在除了SQL语句的情况下面使用的. java.sql.Date是针对SQL语句使用的,它只包含日期而没有时间部分 它们都有getTime方法返回毫秒数,自然就可以直接构建 ...
- java.util.Date与 java.sql.Date两个包下Date的区别与联系
一般我在项目中导入需要的包,大部分情况下都会根据IDE的提示自动导入默认的包,少数情况下会扫一眼,看看是不是需要的包,极少情况会斟酌一下,确认一下这就是需要的包. 最近在项目中导入JDK中的Date包 ...
- mybatis异常invalid comparison: java.util.Date and java.lang.String
原文链接:http://blog.csdn.net/wanghailong_qd/article/details/50673144 mybatis异常invalid comparison: java. ...
- java.util.Date和java.sql.Date 一点区别
最近无意中发现,在oracle中同一样的一个Date类型字段,存储的日期格式有两种不同的情况,第一种是2011-1-1 12:00:00,第二种是2011-1-1,仔细查找发现在向数据库中写数据的时候 ...
- java.util.Date和java.sql.Date的区别及应用
ava.util.Date 就是在除了SQL语句的情况下面使用 java.sql.Date 是针对SQL语句使用的,它只包含日期而没有时间部分 它都有getTime方法返回毫秒数,自然就可以直接构建 ...
最新文章
- 开学无望!13家高校宣布:期末考试线上进行!
- linux查找技巧: find grep xargs linux系统信息查看大全
- H3C交换机S5500系列恢复控制台登录口令
- 基于mysql的主从复制之Mycat简单配置和高可用
- 将本地源代码程序推送远程Github仓库
- STM32的AFIO时钟什么时候开启?
- BaseAnimation
- MAVEN打包时没有将src/main/cache文件夹打到到WAR包中
- LabVIEW树形控件
- Windows修改本地域名解析文件hosts
- springboot 启动banner 打印 佛祖保佑
- QPushButton去掉虚线框(焦点框)
- PDPS软件:3D空间扫描功能介绍与使用方法
- 计算机网络-网络层篇-BGP协议
- Win7完全卸载MySQL的步骤
- 今日头条一面笔试面试题!!!!!完整
- 基于nodejs电影交流网站设计与实现-计算机毕业设计源码+LW文档
- 安全生产工作汇报PPT模板
- 用计算机弹的九八k简谱,作曲 编曲 配器 录音 混音 母带 MIDI 电脑音乐制作(超级全能宝典)...
- 计算地球上2点之间的距离