Skip to content

Instantly share code, notes, and snippets.

@javamultiplex
Created September 23, 2017 16:08
How to convert ZonedDateTime to Date in Java?
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);
}
}
@imthakurrohit
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment