Skip to content

Instantly share code, notes, and snippets.

@invasionofsmallcubes
Created January 3, 2018 17:36
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 invasionofsmallcubes/428d7961ee46b2cf4fcbdb6dfb368824 to your computer and use it in GitHub Desktop.
Save invasionofsmallcubes/428d7961ee46b2cf4fcbdb6dfb368824 to your computer and use it in GitHub Desktop.
Timezone Poc
package com.time;
import org.junit.Test;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
import static java.time.LocalDateTime.ofInstant;
import static java.time.temporal.ChronoUnit.HOURS;
import static java.time.temporal.ChronoUnit.MINUTES;
public class TimezoneTest {
@Test
public void timezoneTest() {
// from orderone
String date = "2018-01-04T10:28";
// from AIRPORTS in OLSON TIMEZONE
String timezone = "Australia/Brisbane";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
LocalDateTime parsedDate = LocalDateTime.parse(date, dateTimeFormatter);
TimeZone timeZone = TimeZone.getDefault();
System.out.println("Host timezone: " + timeZone.getDisplayName() + " (" + timeZone.getID() + ")");
System.out.println("Flight departure: " + parsedDate);
ZoneId brisbaneTimezone = ZoneId.of(timezone);
ZonedDateTime brisbaneDateTime = parsedDate.atZone(brisbaneTimezone);
System.out.println("Flight departure at Brisbane timezone: " + brisbaneDateTime);
System.out.println("Flight departure at Brisbane timezone to Instant: " + brisbaneDateTime.toInstant());
Instant now = Instant.now();
System.out.println("Instant now: " + now);
LocalDateTime localDateTime = ofInstant(now, brisbaneTimezone);
System.out.println("Instant now at " + timezone + ": " + localDateTime);
long minutesUntileFlightFromNow = localDateTime.until(brisbaneDateTime, MINUTES);
long hoursUntileFlightFromNow = localDateTime.until(brisbaneDateTime, HOURS);
long minutesLeft = minutesUntileFlightFromNow - (hoursUntileFlightFromNow * 60);
System.out.println(minutesUntileFlightFromNow + " minutes (" + hoursUntileFlightFromNow + "h" + minutesLeft + "m) hours from departure");
/*
Host timezone: Central European Time (Europe/Rome)
Flight departure: 2018-01-04T10:28
Flight departure at Brisbane timezone: 2018-01-04T10:28+10:00[Australia/Brisbane]
Flight departure at Brisbane timezone to Instant: 2018-01-04T00:28:00Z
Instant now: 2018-01-03T17:34:46.066Z
Instant now at Australia/Brisbane: 2018-01-04T03:34:46.066
413 minutes (6h53m) hours from departure
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment