Skip to content

Instantly share code, notes, and snippets.

@javamultiplex
Created September 23, 2017 16:04
Show Gist options
  • Save javamultiplex/c27921a7f0b45ca11261610bc77e1c2e to your computer and use it in GitHub Desktop.
Save javamultiplex/c27921a7f0b45ca11261610bc77e1c2e to your computer and use it in GitHub Desktop.
How to convert Date to ZonedDateTime in Java?
package com.javamultiplex.datetime;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
/**
*
* @author Rohit Agarwal
* @category Date and Time
* @problem How to convert Date to ZonedDateTime?
*
*/
public class ConvertDateToZonedDateTime {
public static void main(String[] args) {
Date date = new Date();
System.out.println("Date : " + date);
// Get system default time zone id.
ZoneId defaultZoneId = ZoneId.systemDefault();
// Convert Date to Instant.
Instant instant = date.toInstant();
// Instant + default time zone.
ZonedDateTime zonedDateTime = instant.atZone(defaultZoneId);
System.out.println("ZonedDateTime : " + zonedDateTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment