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

In this article, we will learn how to convert java.util.Date to LocalTime in different ways

java.util.Date to LocalTime conversion :

There are many ways to convert java.util.Date to LocalTime –

  1. Using java.sql.Date & Timestamp & toLocalDateTime() & toLocalTime() methods
  2. Using Timestamp & toLocalDateTime() & toLocalTime() methods
  3. Using LocalDateTime.parse() & toLocalTime() methods
  4. Using GregorianCalendar and ZonedDateTime & toLocalTime() method
  5. Using Instant and ZonedDateTime & toLocalTime() method
  6. Using ZonedDateTime and Instant.ofEpochMilli() & toLocalTime() methods

DifferentWaysToConvertJavaUtilDateToLocalTime.java

package in.bench.resources.java8.localtime.examples;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DifferentWaysToConvertJavaUtilDateToLocalTime {

	public static void main(String[] args) {

		// get current date
		Date date = new Date();
		System.out.println("Current Date/time is = " + date);


		// print to console
		System.out.println("\njava.util.Date to java.time.LocalTime conversion :-");
		System.out.println("-----------------------------------------------------");


		// 1. Using java.sql.Date & Timestamp & toLocalDateTime() & toLocalTime() methods
		java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
		Timestamp timestamp1 = new java.sql.Timestamp(sqlDate.getTime());
		LocalDateTime localDateTime1 = timestamp1.toLocalDateTime();
		LocalTime localTime1 = localDateTime1.toLocalTime();
		System.out.println("1. Using java.sql.Date & Timestamp & toLocalDateTime() "
				+ "& toLocalTime() methods :- \n" 
				+ localTime1);


		// 2. Using Timestamp & toLocalDateTime() & toLocalTime() methods
		Date date2 = new Date();
		Timestamp timestamp2 = new Timestamp(date2.getTime());
		LocalDateTime localDateTime2 = timestamp2.toLocalDateTime();
		LocalTime localTime2 = localDateTime2.toLocalTime();
		System.out.println("\n2. Using Timestamp & toLocalDateTime() "
				+ "& toLocalTime() methods :- \n" 
				+ localTime2);


		// 3. Using LocalDateTime.parse() & toLocalTime() methods
		Date date3 = new Date();
		LocalDateTime localDateTime3 = LocalDateTime.parse(
				date3.toString(),
				DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy")
				);
		LocalTime localTime3 = localDateTime3.toLocalTime();
		System.out.println("\n3. Using LocalDateTime.parse() "
				+ "& toLocalTime() methods :- \n" 
				+ localTime3);


		// 4. Using GregorianCalendar and ZonedDateTime & toLocalTime() method
		Date date4 = new Date();
		GregorianCalendar gregorianCalendar = (GregorianCalendar) Calendar.getInstance();
		gregorianCalendar.setTime(date4);
		ZonedDateTime zonedDateTime = gregorianCalendar.toZonedDateTime();
		LocalTime localTime4 = zonedDateTime.toLocalTime();
		System.out.println("\n4. Using GregorianCalendar and ZonedDateTime "
				+ "& toLocalTime() method :- \n" 
				+ localTime4);


		// 5. Using Instant and ZonedDateTime & toLocalTime() method
		Date date5 = new Date();
		Instant instant5 = date5.toInstant();
		ZonedDateTime zonedDateTime5 = instant5.atZone(ZoneId.systemDefault());
		LocalTime localTime5 = zonedDateTime5.toLocalTime();
		System.out.println("\n5. Using Instant and ZonedDateTime "
				+ "& toLocalTime() method :- \n"
				+ localTime5);


		// 6. Using ZonedDateTime and Instant.ofEpochMilli() & toLocalTime() methods
		Date date6 = new Date();
		Instant instant6 = Instant.ofEpochMilli(date6.getTime());
		ZonedDateTime zonedDateTime6 = instant6.atZone(ZoneId.systemDefault());
		LocalTime localTime6 = zonedDateTime6.toLocalTime();
		System.out.print("\n6. Using ZonedDateTime and Instant.ofEpochMilli() "
				+ "& toLocalTime() methods :- \n"
				+ localTime6);
	}
}

Output:

Current Date/time is = Wed Aug 24 10:57:47 IST 2022

java.util.Date to java.time.LocalTime conversion :-
-----------------------------------------------------
1. Using java.sql.Date & Timestamp & toLocalDateTime() & toLocalTime() methods :- 
10:57:47.189

2. Using Timestamp & toLocalDateTime() & toLocalTime() methods :- 
10:57:47.192

3. Using LocalDateTime.parse() & toLocalTime() methods :- 
10:57:47

4. Using GregorianCalendar and ZonedDateTime & toLocalTime() method :- 
10:57:47.312

5. Using Instant and ZonedDateTime & toLocalTime() method :- 
10:57:47.330

6. Using ZonedDateTime and Instant.ofEpochMilli() & toLocalTime() methods :- 
10:57:47.330

Related Articles:



References:



Happy Coding !!
Happy Learning !!

Java 9 – How to convert java.util.Date to LocalDate using ofInstant() method ?
Java 9 – How to convert Instant to LocalTime using ofInstant() method ?