In this article, we will learn how to convert java.util.Date to LocalDate in different ways
java.util.Date to LocalDate conversion :
There are many ways to convert java.util.Date to LocalDate –
- Using java.sql.Date & toLocalDate() method
- Using java.sql.Date & LocalDate.of() method
- Using SimpleDateFormat & LocalDate.parse() method
- Using GregorianCalendar and ZonedDateTime
- Using Instant & ZonedDateTime
- Using Instant.ofEpochMilli() method & ZonedDateTime
DifferentWaysToConvertJavaUtilDateToLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DifferentWaysToConvertJavaUtilDateToLocalDate {
public static void main(String[] args) {
// get current date
Date date = new Date();
System.out.println("Current Date is = " + date);
// print to console
System.out.println("\njava.util.Date to java.time.LocalDate conversion :-");
System.out.println("-----------------------------------------------------");
// 1. Using java.sql.Date & toLocalDate method
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
LocalDate localDate1 = sqlDate.toLocalDate();
System.out.println("1. Using java.sql.Date & toLocalDate method :- \n"
+ localDate1);
// 2. Using java.sql.Date & LocalDate.of() method
Date date2 = new java.sql.Date(new java.util.Date().getTime());
LocalDate localDate2 = LocalDate.of(
date2.getYear() + 1900,
date2.getMonth() + 1,
date2.getDate()
);
System.out.println("\n2. Using java.sql.Date & LocalDate.of() method :- \n"
+ localDate2);
// 3. Using SimpleDateFormat & LocalDate.parse() method
Date date3 = new Date();
LocalDate localDate3 = LocalDate.parse(
new SimpleDateFormat("yyyy-MM-dd").format(date3)
);
System.out.println("\n3. Using SimpleDateFormat & LocalDate.parse() method :- \n"
+ localDate3);
// 4. Using GregorianCalendar and ZonedDateTime
Date date4 = new Date();
GregorianCalendar gregorianCalendar = (GregorianCalendar) Calendar.getInstance();
gregorianCalendar.setTime(date4);
ZonedDateTime zonedDateTime = gregorianCalendar.toZonedDateTime();
LocalDate localDate4 = zonedDateTime.toLocalDate();
System.out.println("\n3. Using GregorianCalendar and ZonedDateTime :- \n"
+ localDate4);
// 5. Using Instant & ZonedDateTime
Date date5 = new Date();
Instant instant5 = date5.toInstant();
ZonedDateTime zonedDateTime5 = instant5.atZone(ZoneId.systemDefault());
LocalDate localDate5 = zonedDateTime5.toLocalDate();
System.out.println("\n5. Using Instant & ZonedDateTime :- \n" + localDate5);
// 6. Using Instant & ZonedDateTime
Date date6 = new Date();
Instant instant6 = Instant.ofEpochMilli(date6.getTime());
ZonedDateTime zonedDateTime6 = instant6.atZone(ZoneId.systemDefault());
LocalDate localDate6 = zonedDateTime6.toLocalDate();
System.out.print("\n6. Using Instant.ofEpochMilli() method & ZonedDateTime :- \n"
+ localDate6);
}
}
Output:
Current Date is = Wed Aug 10 15:24:53 IST 2022
java.util.Date to java.time.LocalDate conversion :-
-----------------------------------------------------
1. Using java.sql.Date & toLocalDate method :-
2022-08-10
2. Using java.sql.Date & LocalDate.of() method :-
2022-08-10
3. Using SimpleDateFormat & LocalDate.parse() method :-
2022-08-10
3. Using GregorianCalendar and ZonedDateTime :-
2022-08-10
5. Using Instant & ZonedDateTime :-
2022-08-10
6. Using Instant.ofEpochMilli() method & ZonedDateTime :-
2022-08-10
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/sql/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html
- https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
Happy Coding !!
Happy Learning !!