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 –
- Using java.sql.Date & Timestamp & toLocalDateTime() & toLocalTime() methods
- Using Timestamp & toLocalDateTime() & toLocalTime() methods
- Using LocalDateTime.parse() & toLocalTime() methods
- Using GregorianCalendar and ZonedDateTime & toLocalTime() method
- Using Instant and ZonedDateTime & toLocalTime() method
- 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:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html
- https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
Happy Coding !!
Happy Learning !!