Created
September 23, 2017 16:04
-
-
Save javamultiplex/c27921a7f0b45ca11261610bc77e1c2e to your computer and use it in GitHub Desktop.
How to convert Date to ZonedDateTime in Java?
This file contains 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.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