Esempio n. 1
0
  private static void conversionOldCalendarToNewZonedDateTime() {
    System.out.println("\nConversion old Calendar to new ZonedDateTime");
    GregorianCalendar oldGregorianCalendar = new GregorianCalendar();

    ZonedDateTime newZonedDateTimeFromGregorianCalendar = oldGregorianCalendar.toZonedDateTime();

    System.out.println("Old Gregorian Calendar: " + oldGregorianCalendar);
    System.out.println(
        "New ZonedDateTime from Gregorian Calendar: " + newZonedDateTimeFromGregorianCalendar);
  }
Esempio n. 2
0
 public void test_toZonedDateTime_fixedZoned() {
   GregorianCalendar gcal = new GregorianCalendar(TimeZone.getTimeZone("GMT+12:00"));
   gcal.set(2008, 0, 1, 0, 0, 0);
   gcal.set(Calendar.MILLISECOND, 0);
   for (int i = 0; i < 500; i++) {
     ZonedDateTime test = gcal.toZonedDateTime();
     assertEquals(test.getYear(), gcal.get(Calendar.YEAR));
     assertEquals(test.getMonthOfYear().getValue(), gcal.get(Calendar.MONTH) + 1);
     assertEquals(test.getDayOfMonth(), gcal.get(Calendar.DATE));
     assertEquals(test.toLocalTime().toNanoOfDay(), 0);
     assertEquals(test.getYear(), i < 366 ? 2008 : 2009);
     assertEquals(test.getOffset().getID(), "+12:00");
     assertEquals(test.getZone().getID(), "UTC+12:00");
     gcal.add(Calendar.DATE, 1);
   }
 }
Esempio n. 3
0
 public void test_toZonedDateTime_variableZoned() {
   GregorianCalendar gcal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris"));
   gcal.set(2008, 0, 1, 0, 0, 0);
   gcal.set(Calendar.MILLISECOND, 0);
   for (int i = 0; i < 500; i++) {
     ZonedDateTime test = gcal.toZonedDateTime();
     assertEquals(test.getYear(), gcal.get(Calendar.YEAR));
     assertEquals(test.getMonthOfYear().getValue(), gcal.get(Calendar.MONTH) + 1);
     assertEquals(test.getDayOfMonth(), gcal.get(Calendar.DATE));
     assertEquals(test.toLocalTime().toNanoOfDay(), 0);
     assertEquals(test.getYear(), i < 366 ? 2008 : 2009);
     boolean isDST = TimeZone.getTimeZone("Europe/Paris").inDaylightTime(gcal.getTime());
     assertEquals(test.getOffset().getID(), isDST ? "+02:00" : "+01:00");
     assertEquals(test.getZone().getID(), "Europe/Paris");
     gcal.add(Calendar.DATE, 1);
   }
 }
Esempio n. 4
0
 public void test_toZonedDateTime_manualZoned() {
   // since 00:45 and Europe/Paris are incompatible, I suppose this should throw an exception
   GregorianCalendar gcal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris"));
   gcal.set(2008, 0, 1, 0, 0, 0);
   gcal.set(Calendar.MILLISECOND, 0);
   gcal.set(Calendar.ZONE_OFFSET, 30 * 60 * 1000);
   gcal.set(Calendar.DST_OFFSET, 15 * 60 * 1000);
   for (int i = 0; i < 500; i++) {
     ZonedDateTime test = gcal.toZonedDateTime();
     assertEquals(test.getYear(), gcal.get(Calendar.YEAR));
     assertEquals(test.getMonthOfYear().getValue(), gcal.get(Calendar.MONTH) + 1);
     assertEquals(test.getDayOfMonth(), gcal.get(Calendar.DATE));
     assertEquals(test.toLocalTime().toNanoOfDay(), 0);
     assertEquals(test.getYear(), i < 366 ? 2008 : 2009);
     assertEquals(test.getOffset().getID(), "+00:45");
     assertEquals(test.getZone().getID(), "Europe/Paris");
     gcal.add(Calendar.DATE, 1);
     gcal.set(Calendar.ZONE_OFFSET, 30 * 60 * 1000);
     gcal.set(Calendar.DST_OFFSET, 15 * 60 * 1000);
   }
 }
Esempio n. 5
0
  public static void main(String[] args) throws Throwable {

    int N = 10000;
    long t1970 = new java.util.Date(70, 0, 01).getTime();
    Random r = new Random();
    for (int i = 0; i < N; i++) {
      int days = r.nextInt(50) * 365 + r.nextInt(365);
      long secs = t1970 + days * 86400 + r.nextInt(86400);
      int nanos = r.nextInt(NANOS_PER_SECOND);
      int nanos_ms = nanos / 1000000 * 1000000; // millis precision
      long millis = secs * 1000 + r.nextInt(1000);
      LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
      LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
      Instant inst = Instant.ofEpochSecond(secs, nanos);
      Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
      ///////////// java.util.Date /////////////////////////
      Date jud = new java.util.Date(millis);
      Instant inst0 = jud.toInstant();
      if (jud.getTime() != inst0.toEpochMilli() || !jud.equals(Date.from(inst0))) {
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
      }
      // roundtrip only with millis precision
      Date jud0 = Date.from(inst_ms);
      if (jud0.getTime() != inst_ms.toEpochMilli() || !inst_ms.equals(jud0.toInstant())) {
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
      }
      //////////// java.util.GregorianCalendar /////////////
      GregorianCalendar cal = new GregorianCalendar();
      // non-roundtrip of tz name between j.u.tz and j.t.zid
      cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
      cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
      cal.setFirstDayOfWeek(Calendar.MONDAY);
      cal.setMinimalDaysInFirstWeek(4);
      cal.setTimeInMillis(millis);
      ZonedDateTime zdt0 = cal.toZonedDateTime();
      if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli()
          || !cal.equals(GregorianCalendar.from(zdt0))) {
        System.out.println("cal:" + cal);
        System.out.println("zdt:" + zdt0);
        System.out.println("calNew:" + GregorianCalendar.from(zdt0));
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
      }
      inst0 = cal.toInstant();
      if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: gcal -> zdt");
      }
      ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
      GregorianCalendar cal0 = GregorianCalendar.from(zdt);
      if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis()
          || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
        System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
        throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
      }
    }

    ///////////// java.util.TimeZone /////////////////////////
    for (String zidStr : TimeZone.getAvailableIDs()) {
      // TBD: tzdt intergration
      if (zidStr.startsWith("SystemV")
          || zidStr.contains("Riyadh8")
          || zidStr.equals("US/Pacific-New")
          || zidStr.equals("EST")
          || zidStr.equals("HST")
          || zidStr.equals("MST")) {
        continue;
      }
      ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
      if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
        throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
      }
      TimeZone tz = TimeZone.getTimeZone(zidStr);
      // no round-trip for alias and "GMT"
      if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId()))
          && !ZoneId.SHORT_IDS.containsKey(zidStr)
          && !zidStr.startsWith("GMT")) {
        throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
      }
    }
    System.out.println("Passed!");
  }