In this article, we will learn how to convert ZonedDateTime to java.util.Date using toInstant() method of ZonedDateTime provided in Java 1.8 version and vice-versa
Date class in Java 1.8 version :
- There are 2 new methods introduced in Java 1.8 version for Date class, those are
- from(Instant) – This static method obtains an instance of
Date
from anInstant
object - toInstant() – This method converts invoking
Date
object to anInstant
- from(Instant) – This static method obtains an instance of
- Note: Many legacy methods of Date class are deprecated
1. Convert ZonedDateTime to java.util.Date :
- Converting ZonedDateTime to Date is very straightforward as,
- ZoneDateTime class has toInstant() method
- Date.from() method accepts Instant as input-argument and returns java.util.Date
- Converted java.util.Date will have,
- Date & Time & Zone parts same as that of ZonedDateTime
- In short, ZonedDateTime -> Instant -> java.util.Date
- Lets see an example for conversion of ZonedDateTime to java.util.Date in the below illustration
ConvertZonedDateTimeToJavaUtilDate.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.util.Date;
public class ConvertZonedDateTimeToJavaUtilDate {
public static void main(String[] args) {
// 1. get Zoned Date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time is :- \n"
+ zonedDateTime);
// 2. get system default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nDefault System Zone is :- \n"
+ zoneId);
// 3. convert ZonedDateTime to java.util.Date using atZone(ZoneId)
Date date = Date.from(zonedDateTime.toInstant());
System.out.print("\nConversion of ZonedDateTime to java.util.Date is :- \n"
+ date);
}
}
Output:
Zoned Date/time is :-
2022-08-13T16:15:30.889407200+05:30[Asia/Calcutta]
Default System Zone is :-
Asia/Calcutta
Conversion of ZonedDateTime to java.util.Date is :-
Sat Aug 13 16:15:30 IST 2022
2. Convert java.util.Date to ZonedDateTime :
- Get java.util.Date instantiating Date object for conversion to ZonedDateTime
- Conversion steps –
- Convert Date to Instant using toInstant() method
- And then invoke atZone() method passing ZoneId as argument which returns ZonedDateTime
- In short, java.util.Date -> Instant -> ZonedDateTime
- Read Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ? for more details and examples
- Lets see an example for conversion of java.util.Date to ZonedDateTime in the below illustration
ConvertJavaUtilDateToZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
public class ConvertJavaUtilDateToZonedDateTime {
public static void main(String[] args) {
// 1. get current Date/time
Date date = new Date();
System.out.println("Current Date/time is :- \n"
+ date);
// 2. get system default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nDefault System Zone is :- \n"
+ zoneId);
// 3. convert java.util.Date to ZonedDateTime
ZonedDateTime zonedDateTime = date.toInstant().atZone(zoneId);
System.out.print("\nConversion of java.util.Date to ZonedDateTime is :- \n"
+ zonedDateTime);
}
}
Output:
Current Date/time is :-
Sat Aug 13 16:15:44 IST 2022
Default System Zone is :-
Asia/Calcutta
Conversion of java.util.Date to ZonedDateTime is :-
2022-08-13T16:15:44.282+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/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.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/sql/Timestamp.html
Happy Coding !!
Happy Learning !!