In this article, we will learn how to convert LocalDate to Instant using atStartOfDay() method of LocalDate provided in Java 1.8 version
For Instant to LocalDate conversion, read Java 8 – How to convert Instant to LocalDate ?
Convert LocalDate to an Instant:
- First, convert LocalDate to either ZonedDateTime/LocalDateTime using variants of atStartOfDay() method and then to an Instant using toInstant() method
- 1st variant –
- atStartOfDay(ZoneId) – this method takes ZoneId as argument and returns ZonedDateTime
- After this conversion, convert ZonedDateTime to an Instant using toInstant() method of ZonedDateTime (inherited from ChronoZonedDateTime)
- 2nd variant –
- atStartOfDay() – this method takes no argument and returns LocalDateTime
- After this conversion, convert LocalDateTime to an Instant using toInstant(ZoneOffset) method of LocalDateTime (inherited from ChronoLocalDateTime)
- Lets see an example for both conversion of LocalDate to an Instant
1. Convert LocalDate to an Instant via ZonedDateTime :
- First, convert LocalDate to ZonedDateTime using atStartOfDay() method passing ZoneId as argument and then invoke toInstant() method which returns Instant
- 1st conversion LocalDate to ZonedDateTime returns 2022–08–01T00:00+05:30[Asia/Calcutta]
- 2nd conversion ZonedDateTime to an Instant returns 2022–07–31T18:30:00Z
- In short, LocalDate -> ZonedDateTime -> Instant
- Note: Instant always provide instantaneous moment at UTC/GMT in yyyy-MM-ddTHH:mm:ss.nnnZ format
ConvertLocalDateToInstant.java
package in.bench.resources.java8.localdate.examples;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
public class ConvertLocalDateToInstant {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 2. get system default Zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nSystem default Zone is :- \n" + zoneId);
// 3. convert LocalDate to Instant
Instant instant = localDate.atStartOfDay(zoneId).toInstant();
System.out.print("\nConversion of LocalDate to Instant with ZoneId is :- \n"
+ instant);
}
}
Output:
Current System Date is :-
2022-08-01
System default Zone is :-
Asia/Calcutta
Conversion of LocalDate to Instant with ZoneId is :-
2022-07-31T18:30:00Z
2. Convert LocalDate to an Instant via LocalDateTime :
- First, convert LocalDate to LocalDateTime using atStartOfDay() method and then invoke toInstant() method passing ZoneOffset as argument which returns Instant
- 1st conversion LocalDate to LocalDateTime returns 2022–08–01T00:00
- 2nd conversion LocalDateTime to an Instant returns 2022–08–01T00:00:00Z
- In short, LocalDate -> LocalDateTime -> Instant
- Note: Instant always provide instantaneous moment at UTC/GMT in yyyy-MM-ddTHH:mm:ss.nnnZ format
ConvertLocalDateToInstant2.java
package in.bench.resources.java8.localdate.examples;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
public class ConvertLocalDateToInstant2 {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 2. get UTC ZoneOffset
ZoneOffset zoneOffset = ZoneOffset.UTC;
System.out.println("\nUTC ZoneOffset is :- \n" + zoneOffset);
// 3. convert LocalDate to Instant
Instant instant = localDate.atStartOfDay().toInstant(zoneOffset);
System.out.print("\nConversion of LocalDate to Instant with UTC ZoneOffset is :- \n"
+ instant);
}
}
Output:
Current System Date is :-
2022-08-01
UTC ZoneOffset is :-
Z
Conversion of LocalDate to Instant with UTC ZoneOffset is :-
2022-08-01T00:00:00Z
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/chrono/ChronoZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/chrono/ChronoLocalDateTime.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!