Skip to content

Instantly share code, notes, and snippets.

@netoht
Created September 30, 2019 02:28
Show Gist options
  • Save netoht/0d6e49aa5b7d332b78b38fd89baefe11 to your computer and use it in GitHub Desktop.
Save netoht/0d6e49aa5b7d332b78b38fd89baefe11 to your computer and use it in GitHub Desktop.

Revisions

  1. netoht created this gist Sep 30, 2019.
    36 changes: 36 additions & 0 deletions TimeZoneTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.util.TimeZone;
    import java.util.concurrent.TimeUnit;

    public class TimeZoneTest {

    // https://www.worldtimebuddy.com/?pl=1&lid=2643743,3448439,3530597&h=2643743
    public static void main(String[] args) {
    String date = "2019-09-30T05:00:00Z";
    String timezone = "America/Mexico_City";
    Instant instant = Instant.parse(date);
    LocalDateTime tzo = LocalDateTime.ofInstant(instant, ZoneId.of(timezone));
    LocalDateTime dateCalculated = tzo.minusDays(1);
    System.out.println("Timezone: " + timezone);
    System.out.println("Cron UTC: " + date);
    System.out.println("Current tz: " + tzo);
    System.out.println("Will run in: " + dateCalculated.toLocalDate());

    System.out.println("\nTimezone Availables:");
    for (String id : TimeZone.getAvailableIDs()) {
    System.out.println(displayTimeZone(TimeZone.getTimeZone(id)));
    }
    }

    private static String displayTimeZone(TimeZone tz) {
    long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
    long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset()) - TimeUnit.HOURS.toMinutes(hours);
    minutes = Math.abs(minutes);
    if (hours > 0) {
    return String.format("(GMT+%d:%02d) %s", hours, minutes, tz.getID());
    }
    return String.format("(GMT%d:%02d) %s", hours, minutes, tz.getID());
    }
    }