How to convert Joda LocalDate to java.util.Date?

前端 未结 5 673
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 04:06

What is the simplest way to convert a JodaTime LocalDate to java.util.Date object?

相关标签:
5条回答
  • 2020-12-08 04:12

    Try this.

    new Date(localDate.toEpochDay())

    0 讨论(0)
  • 2020-12-08 04:14

    JodaTime

    To convert JodaTime's org.joda.time.LocalDate to java.util.Date, do

    Date date = localDate.toDateTimeAtStartOfDay().toDate();
    

    To convert JodaTime's org.joda.time.LocalDateTime to java.util.Date, do

    Date date = localDateTime.toDate();
    

    JavaTime

    To convert Java8's java.time.LocalDate to java.util.Date, do

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

    To convert Java8's java.time.LocalDateTime to java.util.Date, do

    Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    

    You might be tempted to shorten it with LocalDateTime#toInstant(ZoneOffset), but there isn't a direct API to obtain the system default zone offset.

    To convert Java8's java.time.ZonedDateTime to java.util.Date, do

    Date date = Date.from(zonedDateTime.toInstant());
    
    0 讨论(0)
  • 2020-12-08 04:17

    Since 2.0 version LocalDate has a toDate() method

    Date date = localDate.toDate();
    

    If using version 1.5 - 2.0 use:

    Date date = localDate.toDateTimeAtStartOfDay().toDate();
    

    On older versions you are left with:

    Date date = localDate.toDateMidnight().toDate();
    
    0 讨论(0)
  • 2020-12-08 04:27

    You will need a timezone.

    LocalDate date = ...
    
    Date utilDate = date.toDateTimeAtStartOfDay( timeZone ).toDate( );
    
    0 讨论(0)
  • 2020-12-08 04:33

    Maybe this?

    localDate.toDateTimeAtCurrentTime().toDate();
    
    0 讨论(0)
提交回复
热议问题