Search in sources :

Example 6 with Temporal

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

the class TCKLocalTime method test_with_longTemporalField_notChronoField.

// If the field is not a {@code ChronoField}, then the result of this method
// is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
// passing {@code this} as the argument.
@Test
public void test_with_longTemporalField_notChronoField() {
    final LocalTime result = LocalTime.of(12, 30);
    final LocalTime base = LocalTime.of(15, 45);
    TemporalField field = new TemporalField() {

        public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
            throw new UnsupportedOperationException();
        }

        public ValueRange range() {
            return null;
        }

        public boolean isTimeBased() {
            throw new UnsupportedOperationException();
        }

        public boolean isSupportedBy(TemporalAccessor temporal) {
            throw new UnsupportedOperationException();
        }

        public boolean isDateBased() {
            throw new UnsupportedOperationException();
        }

        public TemporalUnit getRangeUnit() {
            throw new UnsupportedOperationException();
        }

        public long getFrom(TemporalAccessor temporal) {
            throw new UnsupportedOperationException();
        }

        public TemporalUnit getBaseUnit() {
            throw new UnsupportedOperationException();
        }

        public <R extends Temporal> R adjustInto(R temporal, long newValue) {
            assertEquals(temporal, base);
            assertEquals(newValue, 12L);
            @SuppressWarnings("unchecked") R r = (R) result;
            return r;
        }
    };
    LocalTime test = base.with(field, 12L);
    assertSame(test, result);
}
Also used : MINUTE_OF_HOUR(java.time.temporal.ChronoField.MINUTE_OF_HOUR) FOREVER(java.time.temporal.ChronoUnit.FOREVER) TemporalField(java.time.temporal.TemporalField) TemporalAccessor(java.time.temporal.TemporalAccessor) LocalTime(java.time.LocalTime) Temporal(java.time.temporal.Temporal) Test(org.testng.annotations.Test)

Example 7 with Temporal

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

the class TCKOffsetTime method test_adjustInto.

@Test(dataProvider = "adjustInto")
public void test_adjustInto(OffsetTime test, Temporal temporal, Temporal expected, Class<?> expectedEx) {
    if (expectedEx == null) {
        Temporal result = test.adjustInto(temporal);
        assertEquals(result, expected);
    } else {
        try {
            Temporal result = test.adjustInto(temporal);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
Also used : Temporal(java.time.temporal.Temporal) DateTimeException(java.time.DateTimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DateTimeParseException(java.time.format.DateTimeParseException) Test(org.testng.annotations.Test)

Example 8 with Temporal

use of java.time.temporal.Temporal 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 9 with Temporal

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

the class TCKDuration method factory_from_TemporalAmount_DaysNanos.

@Test
public void factory_from_TemporalAmount_DaysNanos() {
    TemporalAmount amount = new TemporalAmount() {

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

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

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

        @Override
        public Temporal subtractFrom(Temporal temporal) {
            throw new UnsupportedOperationException();
        }
    };
    Duration t = Duration.from(amount);
    assertEquals(t.getSeconds(), 23 * 86400);
    assertEquals(t.getNano(), 45);
}
Also used : Temporal(java.time.temporal.Temporal) TemporalUnit(java.time.temporal.TemporalUnit) TemporalAmount(java.time.temporal.TemporalAmount) ArrayList(java.util.ArrayList) Duration(java.time.Duration) Test(org.testng.annotations.Test)

Example 10 with Temporal

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

the class TCKDuration method factory_from_TemporalAmount_Minutes_tooBig.

@Test(expectedExceptions = ArithmeticException.class)
public void factory_from_TemporalAmount_Minutes_tooBig() {
    TemporalAmount amount = new TemporalAmount() {

        @Override
        public long get(TemporalUnit unit) {
            return (Long.MAX_VALUE / 60) + 2;
        }

        @Override
        public List<TemporalUnit> getUnits() {
            return Collections.<TemporalUnit>singletonList(MINUTES);
        }

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

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

Aggregations

Temporal (java.time.temporal.Temporal)24 Test (org.testng.annotations.Test)24 TemporalAmount (java.time.temporal.TemporalAmount)7 TemporalUnit (java.time.temporal.TemporalUnit)7 TemporalAdjuster (java.time.temporal.TemporalAdjuster)6 DateTimeException (java.time.DateTimeException)5 DateTimeParseException (java.time.format.DateTimeParseException)5 ArrayList (java.util.ArrayList)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 LocalDate (java.time.LocalDate)2 LocalTime (java.time.LocalTime)2 OffsetTime (java.time.OffsetTime)2 ChronoLocalDate (java.time.chrono.ChronoLocalDate)2 ChronoPeriod (java.time.chrono.ChronoPeriod)2 UnsupportedTemporalTypeException (java.time.temporal.UnsupportedTemporalTypeException)2 Duration (java.time.Duration)1 LocalDateTime (java.time.LocalDateTime)1 OffsetDateTime (java.time.OffsetDateTime)1 Year (java.time.Year)1 MINUTE_OF_HOUR (java.time.temporal.ChronoField.MINUTE_OF_HOUR)1