How to Convert java.util.Date to java.time.LocalDate




Hi Guys,

Welcome to Java Inspires.

In this post, we will see the program how to convert java.util.Date object to java.time.LocalDate.

Java8Example.java

package com.javainspires.java8example;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

/**
 * 
 * @author Java Inspires
 *
 */
public class Java8Example {

	public static void main(String[] args) {
		
		//create a data object
		Date date = new Date();
		// print date
		System.out.println("Date date : "+date);
		
		// convert date to LocalDate
		LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		
		// print LocalDate
		System.out.println("LocalDate localDate : "+localDate.toString());

	}

}

Run the above program:

Date date : Mon Apr 19 13:54:51 IST 2021
LocalDate localDate : 2021-04-19




Post a Comment

Previous Post Next Post