Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Program to help work out time correspondence between timezones
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TimeTabulator {
public static void main(String[] args) {
tabulate(LocalDate.of(2019, 12, 26),
ZoneId.of("Europe/London"),
List.of(ZoneId.of("America/Vancouver"),
ZoneId.of("Pacific/Auckland")));
}
private static void tabulate(LocalDate referenceDate, ZoneId primaryZone, List<ZoneId> secondaryZones) {
for (LocalDateTime now = LocalDateTime.of(referenceDate, LocalTime.MIDNIGHT); now.toLocalDate().equals(referenceDate); now = now.plusHours(1)) {
ZonedDateTime primaryTime = now.atZone(primaryZone);
System.out.println(Stream.concat(Stream.of(primaryTime),
secondaryZones.stream()
.map(primaryTime::withZoneSameInstant))
.map(time -> formatRelative(time, referenceDate))
.collect(Collectors.joining(" = ")));
}
}
private static String formatRelative(ZonedDateTime time, LocalDate referenceDate) {
long dateOffset = time.toLocalDate().toEpochDay() - referenceDate.toEpochDay();
return String.format("%3s %s %s",
dateOffset == 0 ? "" : String.format("%+dd", dateOffset),
time.toLocalTime(),
time.getZone().getId().split("/")[1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.