Java Code Examples for java.time.temporal.TemporalUnit

The following are top voted examples for showing how to use java.time.temporal.TemporalUnit. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: jdk8u-jdk   File: ChronoLocalDateImpl.java   View source code 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public D plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        switch (f) {
            case DAYS: return plusDays(amountToAdd);
            case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7));
            case MONTHS: return plusMonths(amountToAdd);
            case YEARS: return plusYears(amountToAdd);
            case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
            case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
            case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
            case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return (D) ChronoLocalDate.super.plus(amountToAdd, unit);
}
 
Example 2
Project: openjdk-jdk10   File: TCKZonedDateTime.java   View source code 6 votes vote down vote up
@Test
public void test_isSupported_TemporalUnit() {
    assertEquals(TEST_DATE_TIME.isSupported((TemporalUnit) null), false);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.NANOS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MICROS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MILLIS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.SECONDS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MINUTES), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.HOURS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.HALF_DAYS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.DAYS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.WEEKS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MONTHS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.YEARS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.DECADES), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.CENTURIES), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MILLENNIA), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.ERAS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.FOREVER), false);
}
 
Example 3
Project: openjdk-jdk10   File: ChronoLocalDateImpl.java   View source code 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public D plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        switch (f) {
            case DAYS: return plusDays(amountToAdd);
            case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7));
            case MONTHS: return plusMonths(amountToAdd);
            case YEARS: return plusYears(amountToAdd);
            case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
            case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
            case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
            case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return (D) ChronoLocalDate.super.plus(amountToAdd, unit);
}
 
Example 4
Project: OpenJSharp   File: ChronoLocalDateImpl.java   View source code 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public D plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        switch (f) {
            case DAYS: return plusDays(amountToAdd);
            case WEEKS: return plusDays(Math.multiplyExact(amountToAdd, 7));
            case MONTHS: return plusMonths(amountToAdd);
            case YEARS: return plusYears(amountToAdd);
            case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
            case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
            case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
            case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return (D) ChronoLocalDate.super.plus(amountToAdd, unit);
}
 
Example 5
Project: jdk8u-jdk   File: TCKZonedDateTime.java   View source code 6 votes vote down vote up
@Test
public void test_isSupported_TemporalUnit() {
    assertEquals(TEST_DATE_TIME.isSupported((TemporalUnit) null), false);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.NANOS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MICROS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MILLIS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.SECONDS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MINUTES), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.HOURS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.HALF_DAYS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.DAYS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.WEEKS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MONTHS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.YEARS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.DECADES), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.CENTURIES), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.MILLENNIA), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.ERAS), true);
    assertEquals(TEST_DATE_TIME.isSupported(ChronoUnit.FOREVER), false);
}
 
Example 6
Project: openjdk-jdk10   File: ChronoLocalDateTimeImpl.java   View source code 6 votes vote down vote up
@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 7
Project: OpenJSharp   File: Duration.java   View source code 6 votes vote down vote up
/**
 * 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 of the period, measured in terms of the unit, positive or negative
 * @param unit  the unit that the period 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 8
Project: openjdk-jdk10   File: Duration.java   View source code 6 votes vote down vote up
/**
 * 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 9
Project: openjdk-jdk10   File: TCKChronoLocalDate.java   View source code 6 votes vote down vote up
@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 10
Project: openjdk-jdk10   File: TCKLocalDate.java   View source code 6 votes vote down vote up
@Test
public void test_isSupported_TemporalUnit() {
    assertEquals(TEST_2007_07_15.isSupported((TemporalUnit) null), false);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.NANOS), false);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MICROS), false);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MILLIS), false);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.SECONDS), false);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MINUTES), false);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.HOURS), false);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.HALF_DAYS), false);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.DAYS), true);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.WEEKS), true);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MONTHS), true);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.YEARS), true);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.DECADES), true);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.CENTURIES), true);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.MILLENNIA), true);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.ERAS), true);
    assertEquals(TEST_2007_07_15.isSupported(ChronoUnit.FOREVER), false);
}
 
Example 11
Project: openjdk-jdk10   File: TCKChronoLocalDateTime.java   View source code 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badPlusTemporalUnitChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalUnit adjuster = new FixedTemporalUnit(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.plus(1, adjuster);
                Assert.fail("TemporalUnit.doPlus plus should have thrown a ClassCastException" + cdt
                        + ", can not be cast to " + cdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.plus(1, adjuster);
            assertEquals(result, cdt2, "WithAdjuster failed to replace date");
        }
    }
}
 
Example 12
Project: openjdk-jdk10   File: TCKPeriod.java   View source code 5 votes vote down vote up
@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");
}
 
Example 13
Project: openjdk-jdk10   File: TCKInstant.java   View source code 5 votes vote down vote up
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(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 = i1.until(i2, unit);
    assertEquals(amount, expected);
}
 
Example 14
Project: openjdk-jdk10   File: MockSimplePeriod.java   View source code 5 votes vote down vote up
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 15
Project: openjdk-jdk10   File: TCKLocalTime.java   View source code 5 votes vote down vote up
@Test
public void test_minus_longTemporalUnit_invalidUnit() {
    for (TemporalUnit unit : INVALID_UNITS) {
        try {
            TEST_12_30_40_987654321.minus(1, unit);
            fail("Unit should not be allowed " + unit);
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 16
Project: openjdk-jdk10   File: ChronoPeriod.java   View source code 5 votes vote down vote up
/**
 * 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 17
Project: jdk8u-jdk   File: TCKPeriod.java   View source code 5 votes vote down vote up
@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");
}
 
Example 18
Project: ProjectAres   File: TimePeriod.java   View source code 5 votes vote down vote up
@Override
public Temporal subtractFrom(Temporal temporal) {
    for(Map.Entry<TemporalUnit, Long> entry : values.entrySet()) {
        temporal = temporal.minus(entry.getValue(), entry.getKey());
    }
    return temporal;
}
 
Example 19
Project: openjdk-jdk10   File: TCKDuration.java   View source code 5 votes vote down vote up
@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 20
Project: openjdk-jdk10   File: ChronoLocalDateTimeImpl.java   View source code 5 votes vote down vote up
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    Objects.requireNonNull(endExclusive, "endExclusive");
    @SuppressWarnings("unchecked")
    ChronoLocalDateTime<D> end = (ChronoLocalDateTime<D>) getChronology().localDateTime(endExclusive);
    if (unit instanceof ChronoUnit) {
        if (unit.isTimeBased()) {
            long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY);
            switch ((ChronoUnit) unit) {
                case NANOS: amount = Math.multiplyExact(amount, NANOS_PER_DAY); break;
                case MICROS: amount = Math.multiplyExact(amount, MICROS_PER_DAY); break;
                case MILLIS: amount = Math.multiplyExact(amount, MILLIS_PER_DAY); break;
                case SECONDS: amount = Math.multiplyExact(amount, SECONDS_PER_DAY); break;
                case MINUTES: amount = Math.multiplyExact(amount, MINUTES_PER_DAY); break;
                case HOURS: amount = Math.multiplyExact(amount, HOURS_PER_DAY); break;
                case HALF_DAYS: amount = Math.multiplyExact(amount, 2); break;
            }
            return Math.addExact(amount, time.until(end.toLocalTime(), unit));
        }
        ChronoLocalDate endDate = end.toLocalDate();
        if (end.toLocalTime().isBefore(time)) {
            endDate = endDate.minus(1, ChronoUnit.DAYS);
        }
        return date.until(endDate, unit);
    }
    Objects.requireNonNull(unit, "unit");
    return unit.between(this, end);
}
 
Example 21
Project: openjdk-jdk10   File: TCKYearMonth.java   View source code 5 votes vote down vote up
@Test(dataProvider="plus_long_TemporalUnit")
public void test_plus_long_TemporalUnit(YearMonth base, long amount, TemporalUnit unit, YearMonth expectedYearMonth, Class<?> expectedEx) {
    if (expectedEx == null) {
        assertEquals(base.plus(amount, unit), expectedYearMonth);
    } else {
        try {
            YearMonth result = base.plus(amount, unit);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
 
Example 22
Project: openjdk-jdk10   File: CopticDate.java   View source code 5 votes vote down vote up
@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 23
Project: openjdk-jdk10   File: TCKInstant.java   View source code 5 votes vote down vote up
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_between(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 = unit.between(i1, i2);
    assertEquals(amount, expected);
}
 
Example 24
Project: xcalendar   File: XCalendarUtils.java   View source code 4 votes vote down vote up
/**
 * 
 */
public static Instant truncatedTo(Date d, TemporalUnit unit) {
	return d.toInstant().truncatedTo(unit);
}
 
Example 25
Project: jdk8u-jdk   File: TCKChronoZonedDateTime.java   View source code 4 votes vote down vote up
@Override
public long get(TemporalUnit unit) {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
Example 26
Project: daelic   File: Interval.java   View source code 4 votes vote down vote up
public static Interval last(long amount, TemporalUnit unit) {
    return last(amount, unit, Clock.systemUTC());
}
 
Example 27
Project: OpenJSharp   File: HijrahDate.java   View source code 4 votes vote down vote up
@Override
public HijrahDate minus(long amountToSubtract, TemporalUnit unit) {
    return super.minus(amountToSubtract, unit);
}
 
Example 28
Project: daelic   File: Interval.java   View source code 4 votes vote down vote up
public static Interval last(long amount, TemporalUnit unit, ZoneId zone) {
    return last(amount, unit, Clock.system(zone));
}
 
Example 29
Project: potato   File: Cookie.java   View source code 4 votes vote down vote up
public Builder setExpires(int duration, TemporalUnit unit) {
    this.cookie.expires = ZonedDateTime.now(ZoneId.of("UTC")).plus(duration, unit);

    return this;
}
 
Example 30
Project: xcalendar   File: XCalendarUtils.java   View source code 4 votes vote down vote up
public static Instant truncatedTo(Instant d, TemporalUnit unit) {
	return d.truncatedTo(unit);
}
 
Example 31
Project: jdk8u-jdk   File: TCKChronoLocalDate.java   View source code 4 votes vote down vote up
@Override
public TemporalUnit getRangeUnit() {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
Example 32
Project: jdk8u-jdk   File: TCKLocalTime.java   View source code 4 votes vote down vote up
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_between(LocalTime time1, LocalTime time2, TemporalUnit unit, long expected) {
    long amount = unit.between(time1, time2);
    assertEquals(amount, expected);
}
 
Example 33
Project: openjdk-jdk10   File: TCKLocalDateTime.java   View source code 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_minus_longTemporalUnit_null() {
    TEST_2007_07_15_12_30_40_987654321.minus(1, (TemporalUnit) null);
}
 
Example 34
Project: jdk8u-jdk   File: LocalTime.java   View source code 4 votes vote down vote up
/**
 * Returns a copy of this time with the specified amount added.
 * <p>
 * This returns a {@code LocalTime}, based on this one, with the amount
 * in terms of the unit added. If it is not possible to add the amount, because the
 * unit is not supported or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoUnit} then the addition is implemented here.
 * The supported fields behave as follows:
 * <ul>
 * <li>{@code NANOS} -
 *  Returns a {@code LocalTime} with the specified number of nanoseconds added.
 *  This is equivalent to {@link #plusNanos(long)}.
 * <li>{@code MICROS} -
 *  Returns a {@code LocalTime} with the specified number of microseconds added.
 *  This is equivalent to {@link #plusNanos(long)} with the amount
 *  multiplied by 1,000.
 * <li>{@code MILLIS} -
 *  Returns a {@code LocalTime} with the specified number of milliseconds added.
 *  This is equivalent to {@link #plusNanos(long)} with the amount
 *  multiplied by 1,000,000.
 * <li>{@code SECONDS} -
 *  Returns a {@code LocalTime} with the specified number of seconds added.
 *  This is equivalent to {@link #plusSeconds(long)}.
 * <li>{@code MINUTES} -
 *  Returns a {@code LocalTime} with the specified number of minutes added.
 *  This is equivalent to {@link #plusMinutes(long)}.
 * <li>{@code HOURS} -
 *  Returns a {@code LocalTime} with the specified number of hours added.
 *  This is equivalent to {@link #plusHours(long)}.
 * <li>{@code HALF_DAYS} -
 *  Returns a {@code LocalTime} with the specified number of half-days added.
 *  This is equivalent to {@link #plusHours(long)} with the amount
 *  multiplied by 12.
 * </ul>
 * <p>
 * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
 * passing {@code this} as the argument. In this case, the unit determines
 * whether and how to perform the addition.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param amountToAdd  the amount of the unit to add to the result, may be negative
 * @param unit  the unit of the amount to add, not null
 * @return a {@code LocalTime} based on this time with the specified amount added, not null
 * @throws DateTimeException if the addition cannot be made
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public LocalTime plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        switch ((ChronoUnit) unit) {
            case NANOS: return plusNanos(amountToAdd);
            case MICROS: return plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
            case MILLIS: return plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
            case SECONDS: return plusSeconds(amountToAdd);
            case MINUTES: return plusMinutes(amountToAdd);
            case HOURS: return plusHours(amountToAdd);
            case HALF_DAYS: return plusHours((amountToAdd % 2) * 12);
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return unit.addTo(this, amountToAdd);
}
 
Example 35
Project: ProjectAres   File: TimePeriod.java   View source code 4 votes vote down vote up
public static TimePeriod ofUnits(Map<TemporalUnit, Long> values) {
    final ImmutableMap.Builder<TemporalUnit, Long> builder = ImmutableMap.builder();
    values.keySet().stream().sorted(Collections.reverseOrder())
          .forEach(unit -> builder.put(unit, values.get(unit)));
    return new TimePeriod(builder.build());
}
 
Example 36
Project: jdk8u-jdk   File: TCKLocalDate.java   View source code 4 votes vote down vote up
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDate date1, LocalDate date2, TemporalUnit unit, long expected) {
    long amount = date1.until(date2, unit);
    assertEquals(amount, expected);
}
 
Example 37
Project: jdk8u-jdk   File: TCKLocalDateTime.java   View source code 4 votes vote down vote up
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt2.until(dt1, unit);
    assertEquals(amount, -expected);
}
 
Example 38
Project: openjdk-jdk10   File: TCKDuration.java   View source code 4 votes vote down vote up
@Test(dataProvider="OfTemporalUnit")
public void factory_of_longTemporalUnit(long amount, TemporalUnit unit, long expectedSeconds, int expectedNanoOfSecond) {
    Duration t = Duration.of(amount, unit);
    assertEquals(t.getSeconds(), expectedSeconds);
    assertEquals(t.getNano(), expectedNanoOfSecond);
}
 
Example 39
Project: openjdk-jdk10   File: TCKOffsetTime.java   View source code 4 votes vote down vote up
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(OffsetTime offsetTime1, OffsetTime offsetTime2, TemporalUnit unit, long expected) {
    long amount = offsetTime1.until(offsetTime2, unit);
    assertEquals(amount, expected);
}
 
Example 40
Project: ARCLib   File: Utils.java   View source code 4 votes vote down vote up
public static Instant plus(Instant time, TemporalUnit unit, int value) {
    return LocalDateTime.ofInstant(time, ZoneOffset.UTC).plus(value, unit).toInstant(ZoneOffset.UTC);
}