- Convert between Java LocalDateTime and Date
- 1. LocalDateTime to Date
- 2. Date to LocalDateTime
- Java Convert LocalDate to Timestamp
- How to convert LocalDate to Timestamp in Java
- Java LocalDate to Instant and Timestamp
- 1. LocalDate to Instant
- 1.1 Using LocalDate.atTime
- 1.2. Using LocalDate.atStartOfDay
- 1.3. Using LocalDateTime.of
- 2. LocalDate to Timestamp
- 3. Timestamp to Instant
- Complete Example
Convert between Java LocalDateTime and Date
This page will provide examples to convert from Java java.time.LocalDateTime to java.util.Date and from java.util.Date to java.time.LocalDateTime . The LocalDateTime , introduced in Java 8, is date-time without time-zone. The Date represents a specific instant in time, with millisecond precision.
1. LocalDateTime to Date
LocalDateTime does not consist a time-zone and Date represents a specific instant in time, with millisecond precision. To convert from LocalDateTime to Date , we need to provide time-zone.
LocalDateTime + Time-Zone = Date
We can convert LocalDateTime to Date with following methods.
1. Using LocalDateTime.atZone() : It combines this date-time with a given time-zone to create a ZonedDateTime . Then this ZonedDateTime can be converted into Instant . The method Date.from() accepts Instant and returns equivalent Date instance.
LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30"); Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); System.out.println(date);
2. Using LocalDateTime.toInstant() : It combines this local date-time and the specified offset to form an Instant .
instant = localDateTime.toInstant(ZoneOffset.UTC); date = Date.from(instant); System.out.println(date);
3. Using Timestamp.valueOf() : It converts a string object in JDBC timestamp escape format to a Timestamp value. The given string date should be in yyyy-MM-dd HH:mm:ss format.
instant = Timestamp.valueOf(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).toInstant(); date = Date.from(instant); System.out.println(date);
Timestamp timestamp = Timestamp.valueOf(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); date = new Date(timestamp.getTime()); System.out.println(date);
Find the example.
LocalDateTimeToDate.java
package com.concretepage; import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.Date; public class LocalDateTimeToDate < public static void main(String[] args) < LocalDateTime localDateTime = LocalDateTime.parse("2019-11-15T13:15:30"); Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); System.out.println(date); instant = localDateTime.toInstant(ZoneOffset.UTC); date = Date.from(instant); System.out.println(date); instant = Timestamp.valueOf(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).toInstant(); date = Date.from(instant); System.out.println(date); Timestamp timestamp = Timestamp.valueOf(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); date = new Date(timestamp.getTime()); System.out.println(date); >>
Fri Nov 15 13:15:30 IST 2019 Fri Nov 15 18:45:30 IST 2019 Fri Nov 15 13:15:30 IST 2019 Fri Nov 15 13:15:30 IST 2019
2. Date to LocalDateTime
We can convert from Date to LocalDateTime in following ways.
1. First convert Date into Instant using Instant.ofEpochMilli() method. Then use LocalDateTime.ofInstant() method to convert Instant into LocalDateTime with given zone id.
Date date = new Date(); LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); System.out.println(localDateTime);
2. First convert Date into Instant then attach zone id using Instant.atZone() that will return ZonedDateTime . Then use ZonedDateTime.toLocalDateTime() to get LocalDateTime instance.
localDateTime = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(); System.out.println(localDateTime);
DateToLocalDateTime.java
package com.concretepage; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class DateToLocalDateTime < public static void main(String[] args) < Date date = new Date(); LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); System.out.println(localDateTime); localDateTime = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(); System.out.println(localDateTime); >>
2019-08-28T20:31:13.473 2019-08-28T20:31:13.473
Java Convert LocalDate to Timestamp
In this Java core tutorial we learn how to convert a java.time.LocalDate object to a java.sql.Timestamp object in Java programming language.
How to convert LocalDate to Timestamp in Java
In Java, with a given LocalDate object we can follow these steps to convert it to Timestamp object.
- Step 1: convert the LocalDate object to LocalDateTime object using LocalDate.atStartOfDay() method.
- Step 2: convert the LocalDateTime object from step 1 to Timestamp object using Timestamp.valueOf(LocalDateTime dateTime) method.
import java.sql.Timestamp; import java.time.LocalDate; import java.time.LocalDateTime; public class ConvertLocalDateToTimestampExample1 public static void main(String. args) LocalDate localDate = LocalDate.now(); // Convert LocalDate object to Timestamp object LocalDateTime localDateTime = localDate.atStartOfDay(); Timestamp timestamp = Timestamp.valueOf(localDateTime); System.out.println("LocalDate: " + localDate); System.out.println("Timestamp: " + timestamp); > >
LocalDate: 2022-05-22 Timestamp: 2022-05-22 00:00:00.0
Java LocalDate to Instant and Timestamp
On this page we will provide how to convert java.time.LocalDate into java.time.Instant and java.sql.Timestamp . The LocalDate represents a date in the format yyyy-MM-dd such as 2019-05-16. The Instant is an instantaneous point on the time-line. The Timestamp is a thin wrapper around java.util.Date that allows the JDBC API to identify this as an SQL TIMESTAMP value.
1. Find the sample code to convert LocalDate to Instant .
LocalDate localDate = LocalDate.parse("2019-05-16"); Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); System.out.println(instant);//2019-05-15T18:30:00Z
LocalDate localDate = LocalDate.parse("2019-05-16"); Timestamp timestamp = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT)); System.out.println(timestamp); //2019-05-16 00:00:00.0
Contents
1. LocalDate to Instant
To convert LocalDate to Instant , we will convert LocalDate into ZonedDateTime or Timestamp and then calling their toInstant() method we get Instant .
1.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 .
Example:
LocalDate localDate = LocalDate.parse("2019-05-16"); Instant instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); System.out.println(instant); //2019-05-15T18:30:00Z
1.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-16"); Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); System.out.println(instant);//2019-05-15T18:30:00Z
Example-2: 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-16"); Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); System.out.println(instant); //2019-05-15T18:30:00Z
1.3. Using LocalDateTime.of
LocalDate localDate = LocalDate.parse("2019-05-16"); Instant instant = LocalDateTime.of(localDate, LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); System.out.println(instant); //2019-05-15T18:30:00Z
2. LocalDate to Timestamp
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-16"); Timestamp timestamp = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT)); System.out.println(timestamp); //2019-05-16 00:00:00.0
LocalDate localDate = LocalDate.parse("2019-05-16"); Timestamp timestamp = Timestamp.valueOf(LocalDateTime.of(localDate, LocalTime.MIDNIGHT)); System.out.println(timestamp); //2019-05-16 00:00:00.0
3. Timestamp to Instant
LocalDate localDate = LocalDate.parse("2019-05-16"); Timestamp timestamp = Timestamp.valueOf(LocalDateTime.of(localDate, LocalTime.MIDNIGHT)); Instant instant = timestamp.toInstant(); System.out.println(instant); //2019-05-15T18:30:00Z
Complete Example
package 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; public class LocalDateDemo < public static void main(String[] args) < LocalDate localDate = LocalDate.parse("2019-05-16"); System.out.println("---Instant---"); Instant instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); System.out.println(instant); instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); System.out.println(instant); instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant(); System.out.println(instant); instant = LocalDateTime.of(localDate, LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); System.out.println(instant); System.out.println("---Timestamp---"); Timestamp timestamp = Timestamp.valueOf(localDate.atTime(LocalTime.MIDNIGHT)); System.out.println(timestamp); timestamp = Timestamp.valueOf(LocalDateTime.of(localDate, LocalTime.MIDNIGHT)); System.out.println(timestamp); System.out.println("---Timestamp to Instant---"); instant = timestamp.toInstant(); System.out.println(instant); >>
---Instant--- 2019-05-15T18:30:00Z 2019-05-15T18:30:00Z 2019-05-15T18:30:00Z 2019-05-15T18:30:00Z ---Timestamp--- 2019-05-16 00:00:00.0 2019-05-16 00:00:00.0 ---Timestamp to Instant--- 2019-05-15T18:30:00Z