Convert Java LocalDate to Date
May 11, 2019
On this page we will provide how to convert java.time.LocalDate
into java.util.Date
. The LocalDate
represents a date in the format yyyy-MM-dd such as 2019-05-08. We can use following method and constructor to create a Date
object.
1. Date.from(instant): Obtains an instance of
Date
from an Instant
object. We can use ZonedDateTime
or Timestamp
to get Instant
. The ZonedDateTime
and Timestamp
can be obtained using LocalDateTime
and LocalDateTime
can be obtained using LocalDate
.
2. Date(long date): Constructor that creates
Date
object and initializes it to represent the specified number of milliseconds. We can use Timestamp.getTime()
to get time in milliseconds.
Contents
1. Using LocalDate.atTime
LocalDate.atTime
method combines this date with a given time to create a LocalDateTime
. The LocalDateTime.atZone
combines this date-time with a time-zone to create a ZonedDateTime
. The ZonedDateTime.toInstant
converts this date-time to an Instant
. Now we will pass this Instant
instance to Date.from
method that will return a java.util.Date
instance.
Example:
LocalDate localDate = LocalDate.parse("2019-05-08"); Instant instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); System.out.println(date); //Wed May 08 00:00:00 IST 2019
2. Using LocalDate.atStartOfDay
Example-1:LocalDate.atStartOfDay(zone)
returns a zoned date-time from this date at the earliest valid time according to the rules in the time-zone.
LocalDate localDate = LocalDate.parse("2019-05-08"); Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); System.out.println(date); //Wed May 08 00:00:00 IST 2019
LocalDate.atStartOfDay()
combines this date with the time of midnight to create a LocalDateTime
at the start of this date.
LocalDate localDate = LocalDate.parse("2019-05-08"); Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); System.out.println(date); //Wed May 08 00:00:00 IST 2019
3. Using LocalDateTime.of
LocalDateTime.of
obtains an instance of LocalDateTime
from a date and time.
Example:
LocalDate localDate = LocalDate.parse("2019-05-08"); Instant instant = LocalDateTime.of(localDate, LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); System.out.println(date); //Wed May 08 00:00:00 IST 2019
4. Using Timestamp.valueOf
Timestamp.valueOf(dateTime)
obtains an instance of Timestamp
from a LocalDateTime
object.
Example-1: Instantiate
LocalDateTime
using LocalDate.atTime
method.
LocalDate localDate = LocalDate.parse("2019-05-08"); Instant instant = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT)).toInstant(); Date date = Date.from(instant); System.out.println(date); //Wed May 08 00:00:00 IST 2019
LocalDateTime
using LocalDateTime.of
method.
LocalDate localDate = LocalDate.parse("2019-05-08"); Instant instant = Timestamp.valueOf(LocalDateTime.of(localDate, LocalTime.MIDNIGHT)).toInstant(); Date date = Date.from(instant); System.out.println(date); //Wed May 08 00:00:00 IST 2019
Date
using Timestamp.getTime()
method.
LocalDate localDate = LocalDate.parse("2019-05-08"); Timestamp timestamp = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT)); Date date = new Date(timestamp.getTime()); System.out.println(date); //Wed May 08 00:00:00 IST 2019
Complete Example
LocalDateToDate.javapackage com.concretepage; import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.util.Date; public class LocalDateToDate { public static void main(String[] args) { LocalDate localDate = LocalDate.parse("2019-05-08"); Instant instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); System.out.println(date); instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); date = Date.from(instant); System.out.println(date); instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); date = Date.from(instant); System.out.println(date); instant = LocalDateTime.of(localDate, LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); date = Date.from(instant); System.out.println(date); instant = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT)).toInstant(); date = Date.from(instant); System.out.println(date); instant = Timestamp.valueOf(LocalDateTime.of(localDate, LocalTime.MIDNIGHT)).toInstant(); date = Date.from(instant); System.out.println(date); Timestamp timestamp = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT)); date = new Date(timestamp.getTime()); System.out.println(date); } }
Wed May 08 00:00:00 IST 2019 Wed May 08 00:00:00 IST 2019 Wed May 08 00:00:00 IST 2019 Wed May 08 00:00:00 IST 2019 Wed May 08 00:00:00 IST 2019 Wed May 08 00:00:00 IST 2019 Wed May 08 00:00:00 IST 2019
References
Java LocalDateJava DateTimeFormatter