Skip to content

Instantly share code, notes, and snippets.

@tomoTaka01
Created July 21, 2014 04:49
Show Gist options
  • Save tomoTaka01/0b9fee7d46e856f41b76 to your computer and use it in GitHub Desktop.
Save tomoTaka01/0b9fee7d46e856f41b76 to your computer and use it in GitHub Desktop.
Printing Daylight Saving Time by Java8
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.stream.IntStream;
/**
* Printing Date Time by Java8(Date & Time).
*
* @author tomo
*/
public class DaylightSavingSample {
public static void main(String[] args) {
System.out.println(String.format("Testing time is %s", ZonedDateTime.now()));
DaylightSavingSample instance = new DaylightSavingSample();
// Daylight Saving Time Start
System.out.println("----- Daylight Saving Time Start -----");
instance.printZonedDateTime(LocalDateTime.of(2014, Month.MARCH, 9, 0, 0));
// Daylight Saving Time End
System.out.println("----- Daylight Saving Time End -----");
instance.printZonedDateTime(LocalDateTime.of(2014, Month.NOVEMBER, 2, 0, 0));
}
private void printZonedDateTime(LocalDateTime baseDatetime) {
ZoneId utcZone = ZoneId.of("UTC");
ZoneId losZone = ZoneId.of("America/Los_Angeles");
ZoneId tokyoZone = ZoneId.of("Asia/Tokyo");
System.out.println(String.format("*** Base Date Time(UTC) is %s ***", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(baseDatetime), utcZone));
IntStream.range(0, 24).forEach(i -> {
LocalDateTime plusHours = baseDatetime.plusHours(i);
ZonedDateTime utcDatetime = ZonedDateTime.of(plusHours, utcZone);
ZonedDateTime losDatetime = ZonedDateTime.of(plusHours, losZone);
ZonedDateTime tokyoDatetime = ZonedDateTime.of(plusHours, tokyoZone);
String utc = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(utcDatetime);
String los = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(losDatetime);
String tokyo = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(tokyoDatetime);
System.out.println(String.format("UTC:%s, LosAngels:%s, Tokyo:%s", utc, los, tokyo));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment