Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Thursday, December 3, 2020

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

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

No comments:

Post a Comment

Please do not add any spam links in the comments section.