Created
May 15, 2021 11:17
-
-
Save gauravkukade/1ef796c53091ddb2a658f3e7dda22366 to your computer and use it in GitHub Desktop.
A java program to get the zone-specific date time using the java.time.ZonedDateTime class. Check the blog post at https://coderolls.com/get-current-date-time-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
import java.time.OffsetDateTime; | |
import java.time.ZoneId; | |
import java.time.ZoneOffset; | |
import java.time.ZonedDateTime; | |
import java.time.format.DateTimeFormatter; | |
/** | |
* A java program to get the zone-specific date time using the java.time.ZonedDateTime class | |
* @author Gaurav Kukade at coderolls.com | |
*/ | |
public class ZonedDateTimeExample { | |
public static void main(String[] args) { | |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); | |
// get system default zone date time, | |
// my system default zone is +05:30 | |
ZonedDateTime now = ZonedDateTime.now(); | |
System.out.println(dateTimeFormatter.format(now)); // 2021/05/15 13:45:55 | |
System.out.println(now.getOffset()); // +05:30 | |
// check the default time zone | |
System.out.println(ZoneOffset.systemDefault()); // Asia/Kolkata | |
System.out.println(OffsetDateTime.now().getOffset()); // +05:30 | |
// get get current date time for Kolkata, India | |
ZonedDateTime indiaDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Kolkata")); | |
System.out.println(dateTimeFormatter.format(indiaDateTime)); // 2021/05/15 13:45:55 | |
System.out.println(indiaDateTime.getOffset()); // +05:30 | |
// get get current date time for Paris , France | |
ZonedDateTime franceDateTime = now.withZoneSameInstant(ZoneId.of("Europe/Paris")); | |
System.out.println(dateTimeFormatter.format(franceDateTime)); // 2021/05/15 10:15:55 | |
System.out.println(franceDateTime.getOffset()); // +02:00 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment