Search in sources :

Example 6 with ChronoLocalDateTime

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

the class TCKChronoLocalDateTime method test_datetime_comparisons.

//-----------------------------------------------------------------------
// isBefore, isAfter, isEqual
//-----------------------------------------------------------------------
@Test(dataProvider = "calendars")
public void test_datetime_comparisons(Chronology chrono) {
    List<ChronoLocalDateTime<?>> dates = new ArrayList<>();
    ChronoLocalDateTime<?> date = chrono.date(LocalDate.of(2013, 1, 1)).atTime(LocalTime.MIN);
    // 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<ChronoLocalDateTime<?>> otherDates = new ArrayList<>();
        Chronology chrono2 = clist[0];
        for (ChronoLocalDateTime<?> d : dates) {
            otherDates.add(chrono2.date(d).atTime(d.toLocalTime()));
        }
        // Now compare  the sequence of original dates with the sequence of converted dates
        for (int i = 0; i < dates.size(); i++) {
            ChronoLocalDateTime<?> a = dates.get(i);
            for (int j = 0; j < otherDates.size(); j++) {
                ChronoLocalDateTime<?> b = otherDates.get(j);
                int cmp = ChronoLocalDateTime.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 + " isAfter " + 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 + " isAfter " + 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 + " isAfter " + b);
                    assertEquals(a.isEqual(b), true, a + " isEqual " + b);
                }
            }
        }
    }
}
Also used : ChronoLocalDateTime(java.time.chrono.ChronoLocalDateTime) 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)

Aggregations

ChronoLocalDateTime (java.time.chrono.ChronoLocalDateTime)6 LocalDateTime (java.time.LocalDateTime)4 Chronology (java.time.chrono.Chronology)3 ThaiBuddhistChronology (java.time.chrono.ThaiBuddhistChronology)3 Test (org.testng.annotations.Test)3 DateTimeException (java.time.DateTimeException)1 LocalTime (java.time.LocalTime)1 ChronoLocalDate (java.time.chrono.ChronoLocalDate)1 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)1 HijrahChronology (java.time.chrono.HijrahChronology)1 IsoChronology (java.time.chrono.IsoChronology)1 JapaneseChronology (java.time.chrono.JapaneseChronology)1 MinguoChronology (java.time.chrono.MinguoChronology)1 TemporalAccessor (java.time.temporal.TemporalAccessor)1 TemporalField (java.time.temporal.TemporalField)1 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 AbstractTCKTest (tck.java.time.AbstractTCKTest)1