Created
September 23, 2017 16:08
How to convert ZonedDateTime to Date in Java?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.datetime; | |
import java.time.Instant; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
import java.time.ZonedDateTime; | |
import java.util.Date; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category Date and Time | |
* @problem how to convert ZonedDateTime to Date? | |
* | |
*/ | |
public class ConvertZonedDateTimeToDate { | |
public static void main(String[] args) { | |
// Get default time zone | |
ZoneId defaultTimeZone = ZoneId.systemDefault(); | |
// Get current date and time. | |
LocalDateTime localDateTime = LocalDateTime.now(); | |
ZonedDateTime zonedDateTime = localDateTime.atZone(defaultTimeZone); | |
System.out.println("ZonedDateTime : " + zonedDateTime); | |
// ZonedDateTime to Instant | |
Instant instant = zonedDateTime.toInstant(); | |
// Convert Instant to Date. | |
Date date = Date.from(instant); | |
System.out.println("Date : " + date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I use a different timezone instead of systemDefault and convert back to Date, it is converting to systemDefault timezone date instead of give time zone date.