$show=/label

Java 8 - Convert Util Date to LocalDate or LocalDateTime and Back

SHARE:

A quick guide to convert java.util.Date to java.time.LocalDate and LocalDateTime and back LocalDate, LocalDateTime to Date examples.

1. Overview

In this tutorial, We'll learn how to convert between java.util.Date to java.time.LocalDateTime and LocalDate in java 8.

If you are using the third party libraries which returns the java util Date and you want to use the java 8 Date time api then you need to convert the Date to LocalDateTime or LocalDate objects.

Basic knowledge on the following Java 8 concepts are required to understand the example programs. These are recommended but not mandatory.

java.util.Date

LocalDate

LocalDateTime

Instant

java-date-to-localdate-and-localdatetime


2. Conversion Between java.util.Date and java.time.LocalDateTime


2.1 Converting java.util.Date to java.time.LocalDateTime


First convert from Date to LocalDateTime using java 8.

Steps:

Step 1: First create the Date object using new Date().

Step 2: Next, Convert the date into Instant object using new java 8 method toInstant() on date object.

Step 3: Need to add the timezone to the Instant object because it does not handle and know about the timezone. So need to add the timezone using atZone(ZoneId.systemDefault()) method. Use ZoneId.systemDefault() method to get the current system timezone object. If time is in the different time zone then we need to use ZoneId.of() method with the valid timezone string parameter.

Step 4: instant.atZone(ZoneId.systemDefault()) method returns a ZonedDateTime object.

Step 5: Finally, Use ZonedDateTime.toLocalDateTime() to convert the zoned date time to LocalDateTime object.

package com.javaprogramto.java8.dates.conversion.dateto;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * Example to convert java.util.Date to java.time.LocalDateTime
 * 
 * @author javaprogramto.com
 */
public class DateToLocalDateTimeExample {

	public static void main(String[] args) {

		// Creating date using util.Date api
		Date currentDate = new Date();

		// converting util date to java 8 Instnat
		Instant instant = currentDate.toInstant();

		// passing the current system timezone to the atZone() method to convert Instant
		// to ZonedDateTime object.
		ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());

		// convert ZonedDateTime to LocalDateTime using toLocalDateTime()
		LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();

		// printing the dates
		System.out.println("java.util.Date : " + currentDate);
		System.out.println("java.time.LocalDateTime : " + localDateTime);
	}
}
 

Output:

java.util.Date : Thu Dec 03 12:55:57 IST 2020
java.time.LocalDateTime : 2020-12-03T12:55:57.125
 

There are another ways to convert using Instant.toEpochMillis() and java.sql.TimeStamp.toLocalDateTime() methods.

package com.javaprogramto.java8.dates.conversion.dateto;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * Example to convert java.util.Date to java.time.LocalDateTime using
 * Instant.ofEpochMillis() and Timestamp.toLcoalDateTime() methods
 * 
 * @author javaprogramto.com
 */
public class DateToLocalDateTimeOtherExample {

	public static void main(String[] args) {

		// Creating date using util.Date api
		Date currentDate = new Date();

		// using instant.ofEpochMilli() method
		LocalDateTime localDateTime = Instant.ofEpochMilli(currentDate.getTime()).atZone(ZoneId.systemDefault())
				.toLocalDateTime();

		// using java.sql.TimeStamp
		LocalDateTime localDateTime2 = new java.sql.Timestamp(currentDate.getTime()).toLocalDateTime();

		// printing the dates
		System.out.println("java.util Date : " + currentDate);
		System.out.println("java.time.LocalDateTime from instat : " + localDateTime);
		System.out.println("java.time.LocalDateTime from TimeStamp : " + localDateTime2);
	}
}

Output:

java.util Date : Thu Dec 03 13:42:31 IST 2020
java.time.LocalDateTime from instat : 2020-12-03T13:42:31.002
java.time.LocalDateTime from TimeStamp : 2020-12-03T13:42:31.002


2.2 Converting java.time.LocalDateTime to java.util.Date


Below example to convert java 8 LocalDateTime to Date.

When you are working with the dates conversion we need to take care about timezones otherwise those will end up in different dates.

We are taking care about timezones. So, We should not see any issues.

Steps:

Step 1: Create LocalDateTime object using LocalDateTime.now() method.

Step 2: Convert LocalDateTime object to ZonedDateTime using atZone() method with ZoneId.systemDefault() timezone.

Step 3: Convert ZonedDateTime to util.Date using Date.from() and ZonedDateTime.toInstant() methods.

Step 4: Finally, Store the date in the util.Date from Date.from() method.


package com.javaprogramto.java8.dates.conversion.dateto;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * Example to convert java.time.LocalDateTime to java.util.Date.
 * 
 * @author javaprogramto.com
 */
public class LocalDateTimeToDateExample {

	public static void main(String[] args) {

		// Creating the LocalDatetime object
		LocalDateTime currentLocalDateTime = LocalDateTime.now();
		
		// converting LocalDateTime to ZonedDateTime with the system timezone
		ZonedDateTime zonedDateTime = currentLocalDateTime.atZone(ZoneId.systemDefault());
		
		// converting ZonedDateTime to Date using Date.from() and ZonedDateTime.toInstant()
		Date utilDate = Date.from(zonedDateTime.toInstant());
		
		// Printing the input and output dates
		System.out.println("currentLocalDateTime : "+currentLocalDateTime);
		System.out.println("utilDate : "+utilDate);
	}
} 


Output:

currentLocalDateTime : 2020-12-03T13:28:09.359669
utilDate : Thu Dec 03 13:28:09 IST 2020


3. Conversion Between java.util.Date and java.time.LocalDate


Next, convert util Date to LocalDate using Instant and convert it back.


3.1 Converting java.util.Date to java.time.LocalDate

Follow the same steps as mentioned in the above section or read inline comments in the below example.

Only difference is finally need to use ZonedDateTime.toLocalDate() to convert the zoned date time to LocalDate object.

package com.javaprogramto.java8.dates.conversion.dateto;

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

public class DateToLocalDateExample {

	public static void main(String[] args) {

		// Creating date using util.Date api
		Date currentDate = new Date();

		// converting util date to java 8 Instnat
		Instant instant = currentDate.toInstant();

		// passing the current system timezone to the atZone() method to convert Instant
		// to ZonedDateTime object.
		ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());

		//convert ZonedDateTime to LocalDate using toLocalDate()
		LocalDate localDate = zonedDateTime.toLocalDate();
		
		// printing the dates
		System.out.println("java.util.Date : "+currentDate);
		System.out.println("java.time.LocalDate : "+localDate);
	}
}

Output:

java.util.Date : Thu Dec 03 13:55:12 IST 2020
java.time.LocalDate : 2020-12-03

Another ways using Instant.toEpochMilli() and java 8 new java.sql.Da.toLocalDate() method.

package com.javaprogramto.java8.dates.conversion.dateto;

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

/**
 * Example to convert java.util.Date to java.time.LocalDateTime using
 * Instant.ofEpochMillis() and sql.Date.toLcoalDateTime() methods
 * 
 * @author javaprogramto.com
 */
public class DateToLocalDateOtherExample {

	public static void main(String[] args) {

		// Creating date using util.Date api
		Date currentDate = new Date();

		// using instant.ofEpochMilli() method
		LocalDate localDate = Instant.ofEpochMilli(currentDate.getTime()).atZone(ZoneId.systemDefault())
				.toLocalDate();

		// using java.sql.TimeStamp
		LocalDate localDate2 = new java.sql.Date(currentDate.getTime()).toLocalDate();

		// printing the dates in LocalDate
		System.out.println("java.util Date : " + currentDate);
		System.out.println("java.time.LocalDate from instat : " + localDate);
		System.out.println("java.time.LocalDate from TimeStamp : " + localDate2);
	}
}

Output:

java.util Date : Thu Dec 03 14:02:02 IST 2020
java.time.LocalDate from instat : 2020-12-03
java.time.LocalDate from TimeStamp : 2020-12-03


3.2 Converting java.time.LocalDate to java.util.Date


Example to convert LocalDate to date in java 8 using Instant.ofEpochMilli() and java.sql.Date.toLocalDate() methods.

package com.javaprogramto.java8.dates.conversion.dateto;

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

/**
 * Example to convert java.time.LocalDate to java.util.Date.
 * 
 * @author javaprogramto.com
 */
public class LocalDateToDateExample {

	public static void main(String[] args) {

		// Creating the LocalDatetime object
		LocalDate currentLocalDate = LocalDate.now();
		
		// converting LocalDateTime to ZonedDateTime with the system timezone
		ZonedDateTime zonedDateTime = currentLocalDate.atStartOfDay(ZoneId.systemDefault());
		
		// converting ZonedDateTime to Date using Date.from() and ZonedDateTime.toInstant()
		Date utilDate = Date.from(zonedDateTime.toInstant());
		
		// Printing the input and output dates
		System.out.println("currentLocalDate  : "+currentLocalDate);
		System.out.println("utilDate : "+utilDate);
	}
}


Output:

currentLocalDate  : 2020-12-03
utilDate : Thu Dec 03 00:00:00 IST 2020


4. Java 9 DateTime To Date Conversion 


Use LocalDate.ofInstant() and LocalDateTime.ofInstant() methods to convert to Date Time api. This method added in java 9 onwards.

Look at the below example using java 9 methods.


package com.javaprogramto.java8.dates.conversion.dateto;

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

/**
 * 
 * Java 9 examples to convert to util.date to LocalDate and LocalDateTime.
 * 
 * @author javaprogramto.com
 *
 */
public class Java9DateConversion {

	public static void main(String[] args) {
		Date currentDate = new Date();

		// Convert Date to LocalDate
		LocalDate localDate = LocalDate.ofInstant(currentDate.toInstant(), ZoneId.systemDefault());

		// Convert Date to LocalDateTime
		LocalDateTime localDateTime = LocalDateTime.ofInstant(currentDate.toInstant(), ZoneId.systemDefault());

		System.out.println("Localdate : " + localDate);
		System.out.println("LocalDateTime : " + localDateTime);
	}
}


Output:

Localdate : 2020-12-03
LocalDateTime : 2020-12-03T14:52:13.212


5. Conclusion

In this article, we've seen how to do conversion between java.util.Date and java.time.LocalDateTime and back in java 8.

GitHub

Subscribe to Telegram Channel for Daily Updates

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,16,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,28,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,93,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,35,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,13,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,105,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,104,java.lang,5,java.util. function,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,1,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,2,Math,1,Matrix,5,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,27,String,58,String Programs,12,String Revese,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java 8 - Convert Util Date to LocalDate or LocalDateTime and Back
Java 8 - Convert Util Date to LocalDate or LocalDateTime and Back
A quick guide to convert java.util.Date to java.time.LocalDate and LocalDateTime and back LocalDate, LocalDateTime to Date examples.
https://1.bp.blogspot.com/-Yi9Bl4SDP0Y/X8iwWOKRw_I/AAAAAAAADHQ/bcVeXOCC-McwpJIWLISlI_TBkekxnKJaQCLcBGAsYHQ/w640-h476/java-date-to-localdate-and-localdatetime.png
https://1.bp.blogspot.com/-Yi9Bl4SDP0Y/X8iwWOKRw_I/AAAAAAAADHQ/bcVeXOCC-McwpJIWLISlI_TBkekxnKJaQCLcBGAsYHQ/s72-w640-c-h476/java-date-to-localdate-and-localdatetime.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/12/java-date-to-localdate-and-localdatetime.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/12/java-date-to-localdate-and-localdatetime.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content