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

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

+49
java date jodatime
Apr 08 '11 at 15:55
source share
5 answers

Jodatime

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

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

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

 Date date = localDateTime.toDate(); 



Javatime

To convert Java8 java.time.LocalDate to java.util.Date do

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

To convert Java8 java.time.LocalDateTime to java.util.Date do

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

You may be tempted to cut it with LocalDateTime#toInstant(ZoneOffset) , but there is no direct API to get the system default zone offset.

To convert Java8 java.time.ZonedDateTime to java.util.Date do

 Date date = Date.from(zonedDateTime.toInstant()); 
+63
Apr 08 2018-11-11T00:
source share

You will need a time zone.

 LocalDate date = ... Date utilDate = date.toDateTimeAtStartOfDay( timeZone ).toDate( ); 
+9
Apr 08 '11 at 16:01
source share

Since version 2.0 LocalDate has a toDate () method

 Date date = localDate.toDate(); 

When using version 1.5 - 2.0, use:

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

In older versions, you stay:

 Date date = localDate.toDateMidnight().toDate(); 
+8
08 Oct '13 at
source share

Maybe this?

 localDate.toDateTimeAtCurrentTime().toDate(); 
+6
Apr 08 '11 at 4:00 p.m.
source share

Try it.

new date (localDate.toEpochDay ())

-2
Jul 23 '14 at 11:35
source share



All Articles