Skip to content

Instantly share code, notes, and snippets.

@rmg007
Created October 21, 2022 11:57
Show Gist options
  • Save rmg007/9ec6a5f8e69fa2d34210967fcae312c5 to your computer and use it in GitHub Desktop.
Save rmg007/9ec6a5f8e69fa2d34210967fcae312c5 to your computer and use it in GitHub Desktop.
TimeZoneId Examples
import java.time.*;
import java.util.Map;
import java.util.Set;
public class TimeZones {
/*
ZoneId Formats:
20:30Z
GMT+02:00
America/Los_Angeles (Area/City)
*/
public static void main(String[] args) {
//System.out.println(LocalDateTime.now());
//System.out.println(Instant.now()); // Zulu time = GMT
//UTC: the primary time standard by which the world regulates clocks and time
Instant nowUtc = Instant.now();
//System.out.println(nowUtc.getEpochSecond()); // Returns: the seconds from the epoch of 1970-01-01T00:00:00Z
//System.out.println(System.currentTimeMillis());
//long startTime = System.currentTimeMillis();
//// some code
//System.out.println(System.currentTimeMillis() - startTime);
// list of zones available
//Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
//availableZoneIds.forEach(System.out::println);
// list of zone shortIDs
Map<String, String> shortIds = ZoneId.SHORT_IDS;
shortIds.forEach((k, v)-> System.out.println(k + " -> " + v));
// Get Your Current ZoneId
//System.out.println(ZoneId.systemDefault());
// Other places ZoneId
ZoneId tokyoTimeZone = ZoneId.of("Asia/Tokyo");
ZoneId calcuttaTimeZone = ZoneId.of("Asia/Calcutta");
ZoneId asiaSingapore = ZoneId.of("Asia/Singapore");
// Date and Time now in my Zone
//System.out.println(ZonedDateTime.now());
// Date and Time now in different time zones
//System.out.println(LocalDateTime.now(tokyoTimeZone));
//System.out.println(ZonedDateTime.now(tokyoTimeZone));
//System.out.println(nowUtc.atZone(tokyoTimeZone));
//System.out.println(ZonedDateTime.now(calcuttaTimeZone));
//System.out.println(Instant.now().atZone(calcuttaTimeZone));
//ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), tokyoTimeZone);
//System.out.println(zonedDateTime);
// If you don't know the time zone, but you know the offset
ZoneOffset offset = ZoneOffset.of("-02:00");
OffsetDateTime offsetDateTime = OffsetDateTime.of(LocalDateTime.now(), offset);
System.out.println(offsetDateTime);
System.out.println(ZoneOffset.MIN);
System.out.println(ZoneOffset.MAX);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment