失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 将java.time.LocalDate转换为java.util.Date类型

将java.time.LocalDate转换为java.util.Date类型

时间:2020-02-01 12:35:23

相关推荐

将java.time.LocalDate转换为java.util.Date类型

本文翻译自:Convert java.time.LocalDate into java.util.Date type

I want to convertjava.time.LocalDateintojava.util.Datetype.我想将java.time.LocalDate转换为java.util.Date类型。Because I want to set the date intoJDateChooser.因为我想将日期设置为JDateChooser。Or is there any date chooser that supportsjava.timedates?或者是否有支持java.time日期的日期选择器?

#1楼

参考:/question/1YCwP/将java-time-LocalDate转换为java-util-Date类型

#2楼

Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

假设您的日期选择器使用系统默认时区将日期转换为字符串。

#3楼

In order to create ajava.util.Datefrom ajava.time.LocalDate, you have to要从java.time.LocalDate创建java.util.Date,您必须这样做

add a time to theLocalDate为LocalDate添加时间interpret the date and time within a time zone解释时区内的日期和时间get the number of seconds / milliseconds sinceepoch获取自纪元以来的秒数/毫秒数create ajava.util.Date创建一个java.util.Date

The code might look as follows:代码可能如下所示:

LocalDate localDate = LocalDate.now();Date date = new Date(localDate.atStartOfDay(ZoneId.of("America/New_York")).toEpochSecond() * 1000);

#4楼

java.time has the Temporal interface which you can use to create Instant objects from most of the the time classes.java.time具有Temporal接口,您可以使用该接口从大多数时间类创建Instant对象。Instant represents milliseconds on the timeline in the Epoch - the base reference for all other dates and times.Instant表示Epoch中时间轴上的毫秒数 - 所有其他日期和时间的基准参考。

We need to convert the Date into a ZonedDateTime, with a Time and a Zone, to do the conversion:我们需要将Date转换为带有时间和区域的ZonedDateTime来进行转换:

LocalDate ldate = ...;Instant instant = Instant.from(ldate.atStartOfDay(ZoneId.of("GMT")));Date date = Date.from(instant);

#5楼

Here's a utility class I use to convert the newerjava.timeclasses tojava.util.Dateobjects and vice versa:这是我用来将较新的java.time类转换为java.util.Date对象的实用程序类,反之亦然:

import java.time.Instant;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.ZoneId;import java.util.Date;public class DateUtils {public static Date asDate(LocalDate localDate) {return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());}public static Date asDate(LocalDateTime localDateTime) {return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());}public static LocalDate asLocalDate(Date date) {return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();}public static LocalDateTime asLocalDateTime(Date date) {return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();}}

Edited based on @Oliv comment.根据@Oliv评论编辑。

#6楼

You can usejava.sql.Date.valueOf()method as:您可以使用java.sql.Date.valueOf()方法:

Date date = java.sql.Date.valueOf(localDate);

No need to add time and time zone info here because they are taken implicitly.无需在此处添加时间和时区信息,因为它们是隐式使用的。

See LocalDate to java.util.Date and vice versa simpliest conversion?请参阅LocalDate到java.util.Date,反之亦然最简单的转换?

如果觉得《将java.time.LocalDate转换为java.util.Date类型》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。