Search in sources :

Example 16 with ChronoZonedDateTime

use of java.time.chrono.ChronoZonedDateTime in project jdk8u_jdk by JetBrains.

the class TestFormatter method testZoneId.

private void testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal) {
    String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]";
    printFmtStr(locale, fmtStr);
    String expected = toZoneIdStr(test(fmtStr, locale, null, cal));
    test(fmtStr, locale, expected, zdt);
    // get a new cal with fixed tz
    Calendar cal0 = Calendar.getInstance();
    cal0.setTimeInMillis(zdt.toInstant().toEpochMilli());
    cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId()));
    expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0));
    if (zdt instanceof ZonedDateTime) {
        OffsetDateTime odt = ((ZonedDateTime) zdt).toOffsetDateTime();
        test(fmtStr, locale, expected, odt);
        test(fmtStr, locale, expected, odt.toOffsetTime());
    }
    // datetime + zid
    fmtStr = "c:[%tc] c:[%1$Tc]";
    printFmtStr(locale, fmtStr);
    expected = toZoneIdStr(test(fmtStr, locale, null, cal));
    test(fmtStr, locale, expected, zdt);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) OffsetDateTime(java.time.OffsetDateTime)

Example 17 with ChronoZonedDateTime

use of java.time.chrono.ChronoZonedDateTime in project jdk8u_jdk by JetBrains.

the class TCKChronoZonedDateTime method test_zonedDateTime_comparisons.

//-----------------------------------------------------------------------
// isBefore, isAfter, isEqual, timeLineOrder()  test a Chronology against the other Chronos
//-----------------------------------------------------------------------
@Test(dataProvider = "calendars")
public void test_zonedDateTime_comparisons(Chronology chrono) {
    List<ChronoZonedDateTime<?>> dates = new ArrayList<>();
    ChronoZonedDateTime<?> date = chrono.date(LocalDate.of(2013, 1, 1)).atTime(LocalTime.MIN).atZone(ZoneOffset.UTC);
    // Insert dates in order, no duplicates
    dates.add(date.minus(1, ChronoUnit.YEARS));
    dates.add(date.minus(1, ChronoUnit.MONTHS));
    dates.add(date.minus(1, ChronoUnit.WEEKS));
    dates.add(date.minus(1, ChronoUnit.DAYS));
    dates.add(date.minus(1, ChronoUnit.HOURS));
    dates.add(date.minus(1, ChronoUnit.MINUTES));
    dates.add(date.minus(1, ChronoUnit.SECONDS));
    dates.add(date.minus(1, ChronoUnit.NANOS));
    dates.add(date);
    dates.add(date.plus(1, ChronoUnit.NANOS));
    dates.add(date.plus(1, ChronoUnit.SECONDS));
    dates.add(date.plus(1, ChronoUnit.MINUTES));
    dates.add(date.plus(1, ChronoUnit.HOURS));
    dates.add(date.plus(1, ChronoUnit.DAYS));
    dates.add(date.plus(1, ChronoUnit.WEEKS));
    dates.add(date.plus(1, ChronoUnit.MONTHS));
    dates.add(date.plus(1, ChronoUnit.YEARS));
    // Check these dates against the corresponding dates for every calendar
    for (Chronology[] clist : data_of_calendars()) {
        List<ChronoZonedDateTime<?>> otherDates = new ArrayList<>();
        //clist[0];
        Chronology chrono2 = IsoChronology.INSTANCE;
        for (ChronoZonedDateTime<?> d : dates) {
            otherDates.add(chrono2.date(d).atTime(d.toLocalTime()).atZone(d.getZone()));
        }
        // Now compare  the sequence of original dates with the sequence of converted dates
        for (int i = 0; i < dates.size(); i++) {
            ChronoZonedDateTime<?> a = dates.get(i);
            for (int j = 0; j < otherDates.size(); j++) {
                ChronoZonedDateTime<?> b = otherDates.get(j);
                int cmp = ChronoZonedDateTime.timeLineOrder().compare(a, b);
                if (i < j) {
                    assertTrue(cmp < 0, a + " compare " + b);
                    assertEquals(a.isBefore(b), true, a + " isBefore " + b);
                    assertEquals(a.isAfter(b), false, a + " ifAfter " + b);
                    assertEquals(a.isEqual(b), false, a + " isEqual " + b);
                } else if (i > j) {
                    assertTrue(cmp > 0, a + " compare " + b);
                    assertEquals(a.isBefore(b), false, a + " isBefore " + b);
                    assertEquals(a.isAfter(b), true, a + " ifAfter " + b);
                    assertEquals(a.isEqual(b), false, a + " isEqual " + b);
                } else {
                    assertTrue(cmp == 0, a + " compare " + b);
                    assertEquals(a.isBefore(b), false, a + " isBefore " + b);
                    assertEquals(a.isAfter(b), false, a + " ifAfter " + b);
                    assertEquals(a.isEqual(b), true, a + " isEqual " + b);
                }
            }
        }
    }
}
Also used : ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) ArrayList(java.util.ArrayList) HijrahChronology(java.time.chrono.HijrahChronology) ThaiBuddhistChronology(java.time.chrono.ThaiBuddhistChronology) IsoChronology(java.time.chrono.IsoChronology) Chronology(java.time.chrono.Chronology) MinguoChronology(java.time.chrono.MinguoChronology) JapaneseChronology(java.time.chrono.JapaneseChronology) Test(org.testng.annotations.Test)

Example 18 with ChronoZonedDateTime

use of java.time.chrono.ChronoZonedDateTime in project jdk8u_jdk by JetBrains.

the class TCKChronoZonedDateTime method test_from_TemporalAccessor.

//-----------------------------------------------------------------------
@Test(dataProvider = "calendars")
public void test_from_TemporalAccessor(Chronology chrono) {
    ZonedDateTime refDateTime = ZonedDateTime.of(2013, 1, 1, 12, 30, 0, 0, ZoneId.of("Europe/Paris"));
    ChronoZonedDateTime<?> dateTime = chrono.zonedDateTime(refDateTime);
    ChronoZonedDateTime<?> test1 = ChronoZonedDateTime.from(dateTime);
    assertEquals(test1, dateTime);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) Test(org.testng.annotations.Test)

Example 19 with ChronoZonedDateTime

use of java.time.chrono.ChronoZonedDateTime in project jdk8u_jdk by JetBrains.

the class TCKMinguoChronology method test_Instant_zonedDateTime.

//-----------------------------------------------------------------------
// zonedDateTime(Instant, ZoneId )
//-----------------------------------------------------------------------
@Test
public void test_Instant_zonedDateTime() {
    OffsetDateTime offsetDateTime = OffsetDateTime.of(2012, 2, 29, 2, 7, 1, 1, OFFSET_PTWO);
    ZonedDateTime zonedDateTime = ZonedDateTime.of(2012, 2, 29, 2, 7, 1, 1, ZONE_PARIS);
    ChronoZonedDateTime<MinguoDate> result = MinguoChronology.INSTANCE.zonedDateTime(offsetDateTime.toInstant(), offsetDateTime.getOffset());
    assertEquals(result.toLocalDate(), MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2012 - YDIFF, 2, 29));
    assertEquals(result.toLocalTime(), LocalTime.of(2, 7, 1, 1));
    result = MinguoChronology.INSTANCE.zonedDateTime(zonedDateTime.toInstant(), zonedDateTime.getOffset());
    assertEquals(result.toLocalDate(), MinguoChronology.INSTANCE.date(MinguoEra.ROC, 2012 - YDIFF, 2, 29));
    assertEquals(result.toLocalTime(), LocalTime.of(2, 7, 1, 1));
}
Also used : MinguoDate(java.time.chrono.MinguoDate) OffsetDateTime(java.time.OffsetDateTime) ZonedDateTime(java.time.ZonedDateTime) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) Test(org.testng.annotations.Test)

Example 20 with ChronoZonedDateTime

use of java.time.chrono.ChronoZonedDateTime in project Bytecoder by mirkosertic.

the class Parsed method resolveFields.

// -----------------------------------------------------------------------
private void resolveFields() {
    // resolve ChronoField
    resolveInstantFields();
    resolveDateFields();
    resolveTimeFields();
    // any lenient date resolution should return epoch-day
    if (fieldValues.size() > 0) {
        int changedCount = 0;
        outer: while (changedCount < 50) {
            for (Map.Entry<TemporalField, Long> entry : fieldValues.entrySet()) {
                TemporalField targetField = entry.getKey();
                TemporalAccessor resolvedObject = targetField.resolve(fieldValues, this, resolverStyle);
                if (resolvedObject != null) {
                    if (resolvedObject instanceof ChronoZonedDateTime) {
                        ChronoZonedDateTime<?> czdt = (ChronoZonedDateTime<?>) resolvedObject;
                        if (zone == null) {
                            zone = czdt.getZone();
                        } else if (zone.equals(czdt.getZone()) == false) {
                            throw new DateTimeException("ChronoZonedDateTime must use the effective parsed zone: " + zone);
                        }
                        resolvedObject = czdt.toLocalDateTime();
                    }
                    if (resolvedObject instanceof ChronoLocalDateTime) {
                        ChronoLocalDateTime<?> cldt = (ChronoLocalDateTime<?>) resolvedObject;
                        updateCheckConflict(cldt.toLocalTime(), Period.ZERO);
                        updateCheckConflict(cldt.toLocalDate());
                        changedCount++;
                        // have to restart to avoid concurrent modification
                        continue outer;
                    }
                    if (resolvedObject instanceof ChronoLocalDate) {
                        updateCheckConflict((ChronoLocalDate) resolvedObject);
                        changedCount++;
                        // have to restart to avoid concurrent modification
                        continue outer;
                    }
                    if (resolvedObject instanceof LocalTime) {
                        updateCheckConflict((LocalTime) resolvedObject, Period.ZERO);
                        changedCount++;
                        // have to restart to avoid concurrent modification
                        continue outer;
                    }
                    throw new DateTimeException("Method resolve() can only return ChronoZonedDateTime, " + "ChronoLocalDateTime, ChronoLocalDate or LocalTime");
                } else if (fieldValues.containsKey(targetField) == false) {
                    changedCount++;
                    // have to restart to avoid concurrent modification
                    continue outer;
                }
            }
            break;
        }
        if (changedCount == 50) {
            // catch infinite loops
            throw new DateTimeException("One of the parsed fields has an incorrectly implemented resolve method");
        }
        // if something changed then have to redo ChronoField resolve
        if (changedCount > 0) {
            resolveInstantFields();
            resolveDateFields();
            resolveTimeFields();
        }
    }
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) Entry(java.util.Map.Entry) TemporalField(java.time.temporal.TemporalField) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeException(java.time.DateTimeException) ChronoLocalDateTime(java.time.chrono.ChronoLocalDateTime) LocalTime(java.time.LocalTime) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime)

Aggregations

ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)24 ZonedDateTime (java.time.ZonedDateTime)16 OffsetDateTime (java.time.OffsetDateTime)10 Test (org.testng.annotations.Test)7 Instant (java.time.Instant)5 LocalTime (java.time.LocalTime)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)5 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)4 Chronology (java.time.chrono.Chronology)4 Test (org.junit.Test)4 DateTimeException (java.time.DateTimeException)3 LocalDateTime (java.time.LocalDateTime)3 ChronoLocalDateTime (java.time.chrono.ChronoLocalDateTime)3 TemporalAccessor (java.time.temporal.TemporalAccessor)3 TemporalField (java.time.temporal.TemporalField)3 Entry (java.util.Map.Entry)3 LocalDate (java.time.LocalDate)2 OffsetTime (java.time.OffsetTime)2 YearMonth (java.time.YearMonth)2 IsoChronology (java.time.chrono.IsoChronology)2