Created
September 30, 2019 02:28
-
-
Save netoht/0d6e49aa5b7d332b78b38fd89baefe11 to your computer and use it in GitHub Desktop.
TimeZoneTest
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.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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment