In this article, we will learn how to convert java.util.Date to ZonedDateTime in different ways in Java 1.8 version
java.util.Date to ZonedDateTime conversion :
There are many ways to convert java.util.Date to ZonedDateTime –
- Using java.sql.Date & Timestamp & atZone() method
- Using Timestamp & atZone() method
- Using LocalDateTime.parse() & atZone() methods
- Using GregorianCalendar and toZonedDateTime() method
- Using Instant & toInstant() method
- Using Instant.ofEpochMilli() & atZone() methods
DifferentWaysToConvertJavaUtilDateToZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
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 DifferentWaysToConvertJavaUtilDateToZonedDateTime {
public static void main(String[] args) {
// get current date
Date date = new Date();
System.out.println("Current Date is = " + date);
// print to console
System.out.println("\njava.util.Date to java.time.ZonedDateTime conversion :-");
System.out.println("-----------------------------------------------------");
// 1. Using java.sql.Date & Timestamp & atZone() method
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();
ZonedDateTime zonedDateTime1 = localDateTime1.atZone(ZoneId.systemDefault());
System.out.println("1. Using java.sql.Date & Timestamp & atZone() method :- \n"
+ zonedDateTime1);
// 2. Using Timestamp & atZone() method
Date date2 = new Date();
Timestamp timestamp2 = new Timestamp(date2.getTime());
LocalDateTime localDateTime2 = timestamp2.toLocalDateTime();
ZonedDateTime zonedDateTime2 = localDateTime2.atZone(ZoneId.systemDefault());
System.out.println("\n2. Using Timestamp & atZone() method :- \n"
+ zonedDateTime2);
// 3. Using LocalDateTime.parse() & atZone() methods
Date date3 = new Date();
LocalDateTime localDateTime3 = LocalDateTime.parse(
date3.toString(),
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy")
);
ZonedDateTime zonedDateTime3 = localDateTime3.atZone(ZoneId.systemDefault());
System.out.println("\n3. Using LocalDateTime.parse() & atZone() methods :- \n"
+ zonedDateTime3);
// 4. Using GregorianCalendar and toZonedDateTime() method
Date date4 = new Date();
GregorianCalendar gregorianCalendar = (GregorianCalendar) Calendar.getInstance();
gregorianCalendar.setTime(date4);
ZonedDateTime zonedDateTime4 = gregorianCalendar.toZonedDateTime();
System.out.println("\n4. Using GregorianCalendar and toZonedDateTime() method :- \n"
+ zonedDateTime4);
// 5. Using Instant & toInstant() method
Date date5 = new Date();
Instant instant5 = date5.toInstant();
ZonedDateTime zonedDateTime5 = instant5.atZone(ZoneId.systemDefault());
System.out.println("\n5. Using Instant & toInstant() method :- \n"
+ zonedDateTime5);
// 6. Using Instant & ZonedDateTime
Date date6 = new Date();
Instant instant6 = Instant.ofEpochMilli(date6.getTime());
ZonedDateTime zonedDateTime6 = instant6.atZone(ZoneId.systemDefault());
System.out.print("\n6. Using Instant.ofEpochMilli() & atZone() methods :- \n"
+ zonedDateTime6);
}
}
Output:
Current Date is = Sat Aug 13 17:50:21 IST 2022
java.util.Date to java.time.ZonedDateTime conversion :-
-----------------------------------------------------
1. Using java.sql.Date & Timestamp & atZone() method :-
2022-08-13T17:50:21.787+05:30[Asia/Calcutta]
2. Using Timestamp & atZone() method :-
2022-08-13T17:50:21.882+05:30[Asia/Calcutta]
3. Using LocalDateTime.parse() & atZone() methods :-
2022-08-13T17:50:21+05:30[Asia/Calcutta]
4. Using GregorianCalendar and toZonedDateTime() method :-
2022-08-13T17:50:22.010+05:30[Asia/Calcutta]
5. Using Instant & toInstant() method :-
2022-08-13T17:50:22.025+05:30[Asia/Calcutta]
6. Using Instant.ofEpochMilli() & atZone() methods :-
2022-08-13T17:50:22.025+05:30[Asia/Calcutta]
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.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/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 !!