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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22929237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:06:40  来源:igfitidea点击:

Convert java.time.LocalDate into java.util.Date type

javajava-8java-time

提问by Kavinda Gehan

I want to convert java.time.LocalDateinto java.util.Datetype. Because I want to set the date into JDateChooser. Or is there any date chooser that supports java.timedates?

我想转换java.time.LocalDatejava.util.Date类型。因为我想将日期设置为JDateChooser. 或者是否有任何支持java.time日期的日期选择器?

采纳答案by JB Nizet

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

That assumes your date chooser uses the system default timezone to transform dates into strings.

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

回答by nosid

In order to create a java.util.Datefrom a java.time.LocalDate, you have to

为了从java.time.LocalDate创建java.util.Date,您必须

  • add a time to the LocalDate
  • interpret the date and time within a time zone
  • get the number of seconds / milliseconds since epoch
  • create a java.util.Date
  • LocalDate添加时间
  • 解释时区内的日期和时间
  • 获取自纪元以来的秒数/毫秒数
  • 创建一个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);

回答by Kevin Sadler

java.time has the Temporal interface which you can use to create Instant objects from most of the the time classes. Instant represents milliseconds on the timeline in the Epoch - the base reference for all other dates and times.

java.time 具有 Temporal 接口,您可以使用它从大多数时间类创建 Instant 对象。Instant 表示 Epoch 中时间轴上的毫秒数 - 所有其他日期和时间的基本参考。

We need to convert the Date into a ZonedDateTime, with a Time and a Zone, to do the conversion:

我们需要将 Date 转换为 ZonedDateTime,带有 Time 和 Zone,以进行转换:

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

回答by Brice Roncace

Here's a utility class I use to convert the newer java.timeclasses to java.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 评论编辑。

回答by George

You can use java.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 反之亦然最简单的转换?

回答by ceklock

This works for me:

这对我有用:

java.util.Date d = new SimpleDateFormat("yyyy-MM-dd").parse(localDate.toString());

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#toString--

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#toString--

回答by William Nguyen

public static Date convertToTimeZone(Date date, String tzFrom, String tzTo) {
    return Date.from(LocalDateTime.ofInstant(date.toInstant(), ZoneId.of(tzTo)).atZone(ZoneId.of(tzFrom)).toInstant());
} 

回答by Artur Luiz Oliveira

Simple

简单的

public Date convertFrom(LocalDate date) {
    return java.sql.Timestamp.valueOf(date.atStartOfDay());
}

回答by gyoder

Kotlin Solution:

科特林解决方案:

1) Paste this extension function somewhere.

1) 将此扩展功能粘贴到某处。

fun LocalDate.toDate(): Date = Date.from(this.atStartOfDay(ZoneId.systemDefault()).toInstant())

2) Use it, and never google this again.

2)使用它,永远不要再谷歌这个。

val myDate = myLocalDate.toDate()

回答by Mahendra Sri

java.util.Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());