Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/
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