Пример #1
0
 private static LocalDate parse(final DateTimeFormatter formatter, final String value) {
   try {
     Instant epoch = Instant.ofEpochMilli(Long.parseLong(value));
     ZonedDateTime zonedDate =
         epoch.atZone(Optional.ofNullable(formatter.getZone()).orElse(ZoneId.systemDefault()));
     return zonedDate.toLocalDate();
   } catch (NumberFormatException ex) {
     return LocalDate.parse(value, formatter);
   }
 }
 /**
  * Check time zones on the IANA time and zone database
  *
  * @param args
  */
 public static void main(String[] args) {
   ZoneId casaZone = ZoneId.of("Africa/Casablanca");
   ZoneId defaultZone = TimeZone.getDefault().toZoneId();
   LocalDate rightNow = LocalDate.now();
   ZonedDateTime zoneDateTime = rightNow.atStartOfDay(casaZone);
   ZoneId parisZone = ZoneId.of("Europe/Paris");
   LocalDateTime rightNowInTime = LocalDateTime.now();
   ZonedDateTime rightNowInTimeInParis = rightNowInTime.atZone(parisZone);
   Instant thisMoment = Instant.now();
   ZonedDateTime thisMomentInCasa = thisMoment.atZone(casaZone);
   ZoneId tokyoZone = ZoneId.of("Asia/Tokyo");
   LocalDate thisInstantInTokyo = rightNowInTime.toLocalDate();
   LocalDateTime timeFromThisMomentInTokyo = LocalDateTime.ofInstant(thisMoment, tokyoZone);
   System.out.println(
       String.format("In Tokyo right now is %s", timeFromThisMomentInTokyo.toString()));
 }
Пример #3
0
  public static void main(String[] args) {
    ZonedDateTime apollo11launch =
        ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0, ZoneId.of("America/New_York"));
    // 1969-07-16T09:32-04:00[America/New_York]
    System.out.println("apollo11launch: " + apollo11launch);

    Instant instant = apollo11launch.toInstant();
    System.out.println("instant: " + instant);

    ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("UTC"));
    System.out.println("zonedDateTime: " + zonedDateTime);

    ZonedDateTime skipped =
        ZonedDateTime.of(
            LocalDate.of(2013, 3, 31), LocalTime.of(2, 30), ZoneId.of("Europe/Berlin"));
    // Constructs March 31 3:30
    System.out.println("skipped: " + skipped);

    ZonedDateTime ambiguous =
        ZonedDateTime.of(
            LocalDate.of(2013, 10, 27), // End of daylight savings time
            LocalTime.of(2, 30),
            ZoneId.of("Europe/Berlin"));
    // 2013-10-27T02:30+02:00[Europe/Berlin]
    ZonedDateTime anHourLater = ambiguous.plusHours(1);
    // 2013-10-27T02:30+01:00[Europe/Berlin]
    System.out.println("ambiguous: " + ambiguous);
    System.out.println("anHourLater: " + anHourLater);

    ZonedDateTime meeting =
        ZonedDateTime.of(
            LocalDate.of(2013, 10, 31), LocalTime.of(14, 30), ZoneId.of("America/Los_Angeles"));
    System.out.println("meeting: " + meeting);
    ZonedDateTime nextMeeting = meeting.plus(Duration.ofDays(7));
    // Caution! Won’t work with daylight savings time
    System.out.println("nextMeeting: " + nextMeeting);
    nextMeeting = meeting.plus(Period.ofDays(7)); // OK
    System.out.println("nextMeeting: " + nextMeeting);
  }
 @Override
 LocalDate convert(Instant birthday) {
   return birthday.atZone(ZoneId.systemDefault()).toLocalDate();
 }