Java 8 – How to convert java.util.Date to LocalDate in different ways ?

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 –

  1. Using java.sql.Date & toLocalDate() method
  2. Using java.sql.Date & LocalDate.of() method
  3. Using SimpleDateFormat & LocalDate.parse() method
  4. Using GregorianCalendar and ZonedDateTime
  5. Using Instant & ZonedDateTime
  6. 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:



References:



Happy Coding !!
Happy Learning !!

Java 8 – How to check whether a LocalDate is Before another LocalDate ?
Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?