In this article, we will learn how to convert LocalDate to java.util.Date using atStartOfDay() method of LocalDate provided in Java 1.8 version and vice-versa
Date class in Java 1.8 version :
- There are 2 new methods introduced in Java 1.8 version for Date class, those are
- from(Instant) – This static method obtains an instance of
Date
from anInstant
object - toInstant() – This method converts invoking
Date
object to anInstant
- from(Instant) – This static method obtains an instance of
- Note: Many legacy methods of Date class are deprecated
1. Convert LocalDate to java.util.Date :
- LocalDate consist of only Date information whereas java.util.Date requires Date, Time and Zone information
- java.util.Date = LocalDate + Time + Zone information
- For LocalDate to java.util.Date conversion, Time-Zone and Time information is required
- Date.from() method accepts Instant as input-argument
- Convert LocalDate to ZonedDateTime using atStartOfDay() method passing ZoneId as argument and then invoke toInstant() method which returns an Instant
- Now, pass above converted Instant from LocalDate to Date.from() method which will return java.util.Date
- Converted java.util.Date will have,
- Date part same as that of LocalDate
- Adding System default Zone information
- Time part fields hour/minute/second values set to 00
- In short, LocalDate -> ZonedDateTime -> Instant -> java.util.Date
- Lets see an example for conversion of LocalDate to java.util.Date in the below illustration
ConvertLocalDateToJavaUtilDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class ConvertLocalDateToJavaUtilDate {
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("\nDefault System Zone is :- \n" + zoneId);
// 3. convert LocalDate to java.util.Date using atStartOfDay(ZoneId zone)
Date date = Date.from(localDate.atStartOfDay(zoneId).toInstant());
System.out.print("\nConversion of LocalDate to java.util.Date is :- \n"
+ date);
}
}
Output:
Current System Date is :-
2022-08-02
Default System Zone is :-
Asia/Calcutta
Conversion of LocalDate to java.util.Date is :-
Tue Aug 02 00:00:00 IST 2022
2. Convert java.util.Date to LocalDate :
- Get java.util.Date instantiating Date object for conversion to LocalDate
- Conversion steps –
- Convert Date to Instant using toInstant() method
- And then invoke atZone() method passing ZoneId as argument
- And then invoke toLocalDate() method which returns LocalDate
- In short, java.util.Date -> Instant -> ZonedDateTime -> LocalDate
- Read Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Lets see an example for conversion of java.util.Date to LocalDate in the below illustration
ConvertJavaUtilDateToLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class ConvertJavaUtilDateToLocalDate {
public static void main(String[] args) {
// 1. get current Date
Date date = new Date();
System.out.println("Current Date is :- \n" + date);
// 2. get system default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nDefault System Zone is :- \n" + zoneId);
// 3. convert java.util.Date to LocalDate
LocalDate localDate = date.toInstant().atZone(zoneId).toLocalDate();
System.out.print("\nConversion of java.util.Date to LocalDate is :- \n"
+ localDate);
}
}
Output:
Current Date is :-
Tue Aug 02 10:33:12 IST 2022
Default System Zone is :-
Asia/Calcutta
Conversion of java.util.Date to LocalDate is :-
2022-08-02
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/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
Happy Coding !!
Happy Learning !!