Search in sources :

Example 1 with TemporalUnit

use of java.time.temporal.TemporalUnit in project jdk8u_jdk by JetBrains.

the class TCKPeriod method factory_from_TemporalAmount_DaysHours.

@Test(expectedExceptions = DateTimeException.class)
public void factory_from_TemporalAmount_DaysHours() {
    TemporalAmount amount = new TemporalAmount() {

        @Override
        public long get(TemporalUnit unit) {
            if (unit == DAYS) {
                return 1;
            } else {
                return 2;
            }
        }

        @Override
        public List<TemporalUnit> getUnits() {
            List<TemporalUnit> list = new ArrayList<>();
            list.add(DAYS);
            list.add(HOURS);
            return list;
        }

        @Override
        public Temporal addTo(Temporal temporal) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Temporal subtractFrom(Temporal temporal) {
            throw new UnsupportedOperationException();
        }
    };
    Period.from(amount);
}
Also used : Temporal(java.time.temporal.Temporal) TemporalUnit(java.time.temporal.TemporalUnit) TemporalAmount(java.time.temporal.TemporalAmount) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 2 with TemporalUnit

use of java.time.temporal.TemporalUnit in project jdk8u_jdk by JetBrains.

the class TCKLocalTime method test_plus_longTemporalUnit_invalidUnit.

@Test
public void test_plus_longTemporalUnit_invalidUnit() {
    for (TemporalUnit unit : INVALID_UNITS) {
        try {
            TEST_12_30_40_987654321.plus(1, unit);
            fail("Unit should not be allowed " + unit);
        } catch (DateTimeException ex) {
        // expected
        }
    }
}
Also used : DateTimeException(java.time.DateTimeException) TemporalUnit(java.time.temporal.TemporalUnit) Test(org.testng.annotations.Test)

Example 3 with TemporalUnit

use of java.time.temporal.TemporalUnit in project jdk8u_jdk by JetBrains.

the class TCKDuration method test_duration_getUnits.

//-----------------------------------------------------------------------
@Test(groups = "{tck}")
public void test_duration_getUnits() {
    Duration duration = Duration.ofSeconds(5000, 1000);
    List<TemporalUnit> units = duration.getUnits();
    assertEquals(units.size(), 2, "Period.getUnits length");
    assertTrue(units.contains(ChronoUnit.SECONDS), "Period.getUnits contains ChronoUnit.SECONDS");
    assertTrue(units.contains(ChronoUnit.NANOS), "contains ChronoUnit.NANOS");
}
Also used : TemporalUnit(java.time.temporal.TemporalUnit) Duration(java.time.Duration) Test(org.testng.annotations.Test)

Example 4 with TemporalUnit

use of java.time.temporal.TemporalUnit in project jdk8u_jdk by JetBrains.

the class TCKPeriod method test_Period_getUnits.

//-----------------------------------------------------------------------
// get units
//-----------------------------------------------------------------------
@Test
public void test_Period_getUnits() {
    Period period = Period.of(2012, 1, 1);
    List<TemporalUnit> units = period.getUnits();
    assertEquals(units.size(), 3, "Period.getUnits should return 3 units");
    assertEquals(units.get(0), ChronoUnit.YEARS, "Period.getUnits contains ChronoUnit.YEARS");
    assertEquals(units.get(1), ChronoUnit.MONTHS, "Period.getUnits contains ChronoUnit.MONTHS");
    assertEquals(units.get(2), ChronoUnit.DAYS, "Period.getUnits contains ChronoUnit.DAYS");
}
Also used : TemporalUnit(java.time.temporal.TemporalUnit) Period(java.time.Period) Test(org.testng.annotations.Test)

Example 5 with TemporalUnit

use of java.time.temporal.TemporalUnit in project jdk8u_jdk by JetBrains.

the class TCKChronoZonedDateTime method test_badMinusTemporalUnitChrono.

@Test(dataProvider = "calendars")
public void test_badMinusTemporalUnitChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalUnit adjuster = new FixedTemporalUnit(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.minus(1, adjuster);
                Assert.fail("TemporalUnit.doPlus minus should have thrown a ClassCastException, " + czdt.getClass() + " can not be cast to " + czdt2.getClass());
            } catch (ClassCastException cce) {
            // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.minus(1, adjuster);
            assertEquals(result, czdt2, "WithAdjuster failed to replace date");
        }
    }
}
Also used : TemporalUnit(java.time.temporal.TemporalUnit) 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) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Aggregations

TemporalUnit (java.time.temporal.TemporalUnit)60 LocalDate (java.time.LocalDate)18 Test (org.junit.Test)18 Test (org.testng.annotations.Test)17 Temporal (java.time.temporal.Temporal)16 TemporalAmount (java.time.temporal.TemporalAmount)15 Chronology (java.time.chrono.Chronology)12 IsoChronology (java.time.chrono.IsoChronology)12 ArrayList (java.util.ArrayList)12 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)6 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)6 DateTimeException (java.time.DateTimeException)6 HijrahChronology (java.time.chrono.HijrahChronology)6 JapaneseChronology (java.time.chrono.JapaneseChronology)6 MinguoChronology (java.time.chrono.MinguoChronology)6 ThaiBuddhistChronology (java.time.chrono.ThaiBuddhistChronology)6 Matcher (java.util.regex.Matcher)6 Pattern (java.util.regex.Pattern)6 Duration (java.time.Duration)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)4