Java LocalDate to Instant and Timestamp

By Arvind Rai, May 11, 2019
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 
2. Find the sample code to convert LocalDate to Timestamp.
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 
Here we will discuss more examples to convert LocalDate into Instant and Timestamp.

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

LocalDateTime.of obtains an instance of LocalDateTime from a date and time.
Example:
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 
Example-2: Instantiate LocalDateTime using LocalDateTime.of method.
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

We can convert Timestamp to Instant using Timestamp.toInstant() method.
Example:
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

LocalDateDemo.java
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);
  }
} 
Output
---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 

References

Java LocalDate to Date
Java LocalDate
Java DateTimeFormatter
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us