What is the simplest way to convert a JodaTime LocalDate
to java.util.Date
object?
Asked
Active
Viewed 6.0k times
65

JodaStephen
- 60,927
- 15
- 95
- 117

Eric Wilson
- 57,719
- 77
- 200
- 270
5 Answers
80
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());
-
1For code safety, consider passing a timezone, because it will use the default one. – Yann Moisan Nov 03 '13 at 20:29
-
how to achieve this -->new LocalDateTime(Long.MAX_VALUE).toDateTimeAtStartOfDay().toDate, which causes overflow on long – Gowrav Jun 22 '17 at 16:58
-
Running in to a very specific issue with this. Our server is located in Malta and I am now handling a case of someone born on 1973-03-31. When converting this with toDateTimeAtStartOfDay() it becomes 1973-03-31 00:00:00.000 which is a moment in time that never existed in Malta because of Daylight Savings Time. I could just add the time as noon and the problem would be solved, but I am wondering if there is a cleaner way to convert this, as I never actually use the time, only the date. – Sander Oct 27 '17 at 09:10
10
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();

maraswrona
- 798
- 6
- 8
9
You will need a timezone.
LocalDate date = ...
Date utilDate = date.toDateTimeAtStartOfDay( timeZone ).toDate( );

Alexander Pogrebnyak
- 44,836
- 10
- 105
- 121
-3
Try this.
new Date(localDate.toEpochDay())

Alex Sales
- 176
- 1
- 7
-
3Whilst toEpochDay() returns a long, and the Date constructor takes a long they are not the same thing. The Epoch Day count is a simple incrementing count of days where day 0 is 1970-01-01 (ISO). Whereas the constructor takes the specified number of milliseconds since the standard base time known as "the epoch". toEpochDay() gives you days, the constructor take milliseconds. – Kevin Sadler Dec 01 '14 at 15:29