Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created May 15, 2021 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravkukade/1ef796c53091ddb2a658f3e7dda22366 to your computer and use it in GitHub Desktop.
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/
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