java.time.temporal.TemporalUnit Java Examples
The following examples show how to use
java.time.temporal.TemporalUnit.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TCKYear.java From TencentKona-8 with GNU General Public License v2.0 | 7 votes |
@Test public void test_isSupported_TemporalUnit() { assertEquals(TEST_2008.isSupported((TemporalUnit) null), false); assertEquals(TEST_2008.isSupported(ChronoUnit.NANOS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.MICROS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.MILLIS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.SECONDS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.MINUTES), false); assertEquals(TEST_2008.isSupported(ChronoUnit.HOURS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.HALF_DAYS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.DAYS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.WEEKS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.MONTHS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.YEARS), true); assertEquals(TEST_2008.isSupported(ChronoUnit.DECADES), true); assertEquals(TEST_2008.isSupported(ChronoUnit.CENTURIES), true); assertEquals(TEST_2008.isSupported(ChronoUnit.MILLENNIA), true); assertEquals(TEST_2008.isSupported(ChronoUnit.ERAS), true); assertEquals(TEST_2008.isSupported(ChronoUnit.FOREVER), false); }
Example #2
Source File: ChronoLocalDateTimeImpl.java From desugar_jdk_libs with GNU General Public License v2.0 | 6 votes |
@Override public ChronoLocalDateTimeImpl<D> plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000); case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusMinutes(amountToAdd); case HOURS: return plusHours(amountToAdd); case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2) } return with(date.plus(amountToAdd, unit), time); } return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), unit.addTo(this, amountToAdd)); }
Example #3
Source File: Duration.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * Returns a copy of this duration with the specified duration added. * <p> * The duration amount is measured in terms of the specified unit. * Only a subset of units are accepted by this method. * The unit must either have an {@linkplain TemporalUnit#isDurationEstimated() exact duration} or * be {@link ChronoUnit#DAYS} which is treated as 24 hours. Other units throw an exception. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount to add, measured in terms of the unit, positive or negative * @param unit the unit that the amount is measured in, must have an exact duration, not null * @return a {@code Duration} based on this duration with the specified duration added, not null * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ public Duration plus(long amountToAdd, TemporalUnit unit) { Objects.requireNonNull(unit, "unit"); if (unit == DAYS) { return plus(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY), 0); } if (unit.isDurationEstimated()) { throw new UnsupportedTemporalTypeException("Unit must not have an estimated duration"); } if (amountToAdd == 0) { return this; } if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusSeconds((amountToAdd / (1000_000L * 1000)) * 1000).plusNanos((amountToAdd % (1000_000L * 1000)) * 1000); case MILLIS: return plusMillis(amountToAdd); case SECONDS: return plusSeconds(amountToAdd); } return plusSeconds(Math.multiplyExact(unit.getDuration().seconds, amountToAdd)); } Duration duration = unit.getDuration().multipliedBy(amountToAdd); return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano()); }
Example #4
Source File: TCKChronoLocalDate.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="calendars") public void test_badMinusTemporalUnitChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDate date = chrono.date(refDate); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDate date2 = chrono2.date(refDate); TemporalUnit adjuster = new FixedTemporalUnit(date2); if (chrono != chrono2) { try { date.minus(1, adjuster); Assert.fail("TemporalUnit.doAdd minus should have thrown a ClassCastException" + date.getClass() + ", can not be cast to " + date2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDate result = date.minus(1, adjuster); assertEquals(result, date2, "WithAdjuster failed to replace date"); } } }
Example #5
Source File: ChronoLocalDateTimeImpl.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
@Override public ChronoLocalDateTimeImpl<D> plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000); case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusMinutes(amountToAdd); case HOURS: return plusHours(amountToAdd); case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2) } return with(date.plus(amountToAdd, unit), time); } return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), unit.addTo(this, amountToAdd)); }
Example #6
Source File: MockSimplePeriod.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private MockSimplePeriod(long amount, TemporalUnit unit) { Objects.requireNonNull(unit, "unit"); if (unit == FOREVER) { throw new DateTimeException("Cannot create a period of the Forever unit"); } this.amount = amount; this.unit = unit; }
Example #7
Source File: ChronoLocalDate.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override default ChronoLocalDate plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return ChronoLocalDateImpl.ensureValid(getChronology(), unit.addTo(this, amountToAdd)); }
Example #8
Source File: ChronoPeriod.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
/** * Checks if any of the supported units of this period are negative. * * @return true if any unit of this period is negative */ default boolean isNegative() { for (TemporalUnit unit : getUnits()) { if (get(unit) < 0) { return true; } } return false; }
Example #9
Source File: ChronoPeriodImpl.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Override public long get(TemporalUnit unit) { if (unit == ChronoUnit.YEARS) { return years; } else if (unit == ChronoUnit.MONTHS) { return months; } else if (unit == ChronoUnit.DAYS) { return days; } else { throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } }
Example #10
Source File: CopticDate.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
@Override public long until(Temporal endExclusive, TemporalUnit unit) { CopticDate end = getChronology().date(endExclusive); if (unit instanceof ChronoUnit) { return LocalDate.from(this).until(end, unit); // TODO: this is wrong } return unit.between(this, end); }
Example #11
Source File: ChronoPeriodImpl.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Override public long get(TemporalUnit unit) { if (unit == ChronoUnit.YEARS) { return years; } else if (unit == ChronoUnit.MONTHS) { return months; } else if (unit == ChronoUnit.DAYS) { return days; } else { throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } }
Example #12
Source File: TCKInstant.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="plusTemporalAmount") public void test_plusTemporalAmount(TemporalUnit unit, TemporalAmount amount, int seconds, int nanos) { Instant inst = Instant.ofEpochMilli(1000); Instant actual = inst.plus(amount); Instant expected = Instant.ofEpochSecond(seconds, nanos); assertEquals(actual, expected, "plus(TemporalAmount) failed"); }
Example #13
Source File: TCKInstant.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="plusTemporalAmount") public void test_plusTemporalAmount(TemporalUnit unit, TemporalAmount amount, int seconds, int nanos) { Instant inst = Instant.ofEpochMilli(1000); Instant actual = inst.plus(amount); Instant expected = Instant.ofEpochSecond(seconds, nanos); assertEquals(actual, expected, "plus(TemporalAmount) failed"); }
Example #14
Source File: TCKInstant.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="periodUntilUnit") public void test_until_TemporalUnit_negated(long seconds1, int nanos1, long seconds2, long nanos2, TemporalUnit unit, long expected) { Instant i1 = Instant.ofEpochSecond(seconds1, nanos1); Instant i2 = Instant.ofEpochSecond(seconds2, nanos2); long amount = i2.until(i1, unit); assertEquals(amount, -expected); }
Example #15
Source File: ChronoLocalDate.java From hottub with GNU General Public License v2.0 | 5 votes |
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override default ChronoLocalDate plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return ChronoLocalDateImpl.ensureValid(getChronology(), unit.addTo(this, amountToAdd)); }
Example #16
Source File: TCKDuration.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@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"); }
Example #17
Source File: ChronoLocalDateImpl.java From jdk1.8-source-analysis with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings("unchecked") public D minus(long amountToSubtract, TemporalUnit unit) { return (D) ChronoLocalDate.super.minus(amountToSubtract, unit); }
Example #18
Source File: TCKChronoLocalDate.java From hottub with GNU General Public License v2.0 | 4 votes |
@Override public TemporalUnit getBaseUnit() { throw new UnsupportedOperationException("Not supported yet."); }
Example #19
Source File: TCKChronoLocalDateTime.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Override public TemporalUnit getRangeUnit() { throw new UnsupportedOperationException("Not supported yet."); }
Example #20
Source File: Instant.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * Calculates the amount of time until another instant in terms of the specified unit. * <p> * This calculates the amount of time between two {@code Instant} * objects in terms of a single {@code TemporalUnit}. * The start and end points are {@code this} and the specified instant. * The result will be negative if the end is before the start. * The calculation returns a whole number, representing the number of * complete units between the two instants. * The {@code Temporal} passed to this method is converted to a * {@code Instant} using {@link #from(TemporalAccessor)}. * For example, the amount in seconds between two dates can be calculated * using {@code startInstant.until(endInstant, SECONDS)}. * <p> * There are two equivalent ways of using this method. * The first is to invoke this method. * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: * <pre> * // these two lines are equivalent * amount = start.until(end, SECONDS); * amount = SECONDS.between(start, end); * </pre> * The choice should be made based on which makes the code more readable. * <p> * The calculation is implemented in this method for {@link ChronoUnit}. * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS}, * {@code MINUTES}, {@code HOURS}, {@code HALF_DAYS} and {@code DAYS} * are supported. Other {@code ChronoUnit} values will throw an exception. * <p> * If the unit is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} * passing {@code this} as the first argument and the converted input temporal * as the second argument. * <p> * This instance is immutable and unaffected by this method call. * * @param endExclusive the end date, exclusive, which is converted to an {@code Instant}, not null * @param unit the unit to measure the amount in, not null * @return the amount of time between this instant and the end instant * @throws DateTimeException if the amount cannot be calculated, or the end * temporal cannot be converted to an {@code Instant} * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override public long until(Temporal endExclusive, TemporalUnit unit) { Instant end = Instant.from(endExclusive); if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case NANOS: return nanosUntil(end); case MICROS: return nanosUntil(end) / 1000; case MILLIS: return Math.subtractExact(end.toEpochMilli(), toEpochMilli()); case SECONDS: return secondsUntil(end); case MINUTES: return secondsUntil(end) / SECONDS_PER_MINUTE; case HOURS: return secondsUntil(end) / SECONDS_PER_HOUR; case HALF_DAYS: return secondsUntil(end) / (12 * SECONDS_PER_HOUR); case DAYS: return secondsUntil(end) / (SECONDS_PER_DAY); } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.between(this, end); }
Example #21
Source File: TemporalDialer.java From freemarker-java-8 with Apache License 2.0 | 4 votes |
public TemporalDialer(Temporal obj, TemporalUnit temporalUnit) { super(obj); this.temporalUnit = temporalUnit; }
Example #22
Source File: TCKLocalDate.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions=NullPointerException.class) public void test_minus_longTemporalUnit_null() { TEST_2007_07_15.minus(1, (TemporalUnit) null); }
Example #23
Source File: HijrahDate.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
@Override public HijrahDate minus(long amountToSubtract, TemporalUnit unit) { return super.minus(amountToSubtract, unit); }
Example #24
Source File: TCKLocalDate.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider="periodUntilUnit") public void test_until_TemporalUnit_negated(LocalDate date1, LocalDate date2, TemporalUnit unit, long expected) { long amount = date2.until(date1, unit); assertEquals(amount, -expected); }
Example #25
Source File: TCKOffsetDateTime.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider="periodUntilUnit") public void test_until_TemporalUnit_negated(OffsetDateTime odt1, OffsetDateTime odt2, TemporalUnit unit, long expected) { long amount = odt2.until(odt1, unit); assertEquals(amount, -expected); }
Example #26
Source File: TCKLocalDateTime.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions=NullPointerException.class) public void test_minus_longTemporalUnit_null() { TEST_2007_07_15_12_30_40_987654321.minus(1, (TemporalUnit) null); }
Example #27
Source File: MockSimplePeriod.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
@Override public long get(TemporalUnit unit) { throw new UnsupportedOperationException("Not supported yet."); }
Example #28
Source File: TCKInstant.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class) public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) { input.truncatedTo(unit); }
Example #29
Source File: TCKChronoLocalDateTime.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
@Override public TemporalUnit getRangeUnit() { throw new UnsupportedOperationException("Not supported yet."); }
Example #30
Source File: DateTimeParserContext.java From jphp with Apache License 2.0 | 4 votes |
@Override public TemporalUnit getBaseUnit() { return null; }