org.threeten.bp.temporal.Temporal Java Examples

The following examples show how to use org.threeten.bp.temporal.Temporal. 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: ChronoPeriodImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    Jdk8Methods.requireNonNull(temporal, "temporal");
    Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
    if (temporalChrono != null && chronology.equals(temporalChrono) == false) {
        throw new DateTimeException("Invalid chronology, required: " + chronology.getId() + ", but was: " + temporalChrono.getId());
    }
    if (years != 0) {
        temporal = temporal.plus(years, YEARS);
    }
    if (months != 0) {
        temporal = temporal.plus(months, MONTHS);
    }
    if (days != 0) {
        temporal = temporal.plus(days, DAYS);
    }
    return temporal;
}
 
Example #2
Source File: ChronoPeriodImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Temporal subtractFrom(Temporal temporal) {
    Jdk8Methods.requireNonNull(temporal, "temporal");
    Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
    if (temporalChrono != null && chronology.equals(temporalChrono) == false) {
        throw new DateTimeException("Invalid chronology, required: " + chronology.getId() + ", but was: " + temporalChrono.getId());
    }
    if (years != 0) {
        temporal = temporal.minus(years, YEARS);
    }
    if (months != 0) {
        temporal = temporal.minus(months, MONTHS);
    }
    if (days != 0) {
        temporal = temporal.minus(days, DAYS);
    }
    return temporal;
}
 
Example #3
Source File: TestLocalTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_with_adjustment() {
    final LocalTime sample = LocalTime.of(23, 5);
    TemporalAdjuster adjuster = new TemporalAdjuster() {
        @Override
        public Temporal adjustInto(Temporal dateTime) {
            return sample;
        }
    };
    assertEquals(TEST_12_30_40_987654321.with(adjuster), sample);
}
 
Example #4
Source File: ChronoZonedDateTimeImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    @SuppressWarnings("unchecked")
    ChronoZonedDateTime<D> end = (ChronoZonedDateTime<D>) toLocalDate().getChronology().zonedDateTime(endExclusive);
    if (unit instanceof ChronoUnit) {
        end = end.withZoneSameInstant(offset);
        return dateTime.until(end.toLocalDateTime(), unit);
    }
    return unit.between(this, end);
}
 
Example #5
Source File: TestOffsetTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_with_adjustment_AmPm() {
    OffsetTime test = TEST_11_30_59_500_PONE.with(new TemporalAdjuster() {
        @Override
        public Temporal adjustInto(Temporal dateTime) {
            return dateTime.with(HOUR_OF_DAY, 23);
        }
    });
    assertEquals(test, OffsetTime.of(LocalTime.of(23, 30, 59, 500), OFFSET_PONE));
}
 
Example #6
Source File: TestOffsetTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_with_adjustment() {
    final OffsetTime sample = OffsetTime.of(LocalTime.of(23, 5), OFFSET_PONE);
    TemporalAdjuster adjuster = new TemporalAdjuster() {
        @Override
        public Temporal adjustInto(Temporal dateTime) {
            return sample;
        }
    };
    assertEquals(TEST_11_30_59_500_PONE.with(adjuster), sample);
}
 
Example #7
Source File: Chronology.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Casts the {@code Temporal} to {@code ChronoLocalDate} with the same chronology.
 *
 * @param temporal  a date-time to cast, not null
 * @return the date-time checked and cast to {@code ChronoLocalDate}, not null
 * @throws ClassCastException if the date-time cannot be cast to ChronoLocalDate
 *  or the chronology is not equal this Chrono
 */
<D extends ChronoLocalDate> D ensureChronoLocalDate(Temporal temporal) {
    @SuppressWarnings("unchecked")
    D other = (D) temporal;
    if (this.equals(other.getChronology()) == false) {
        throw new ClassCastException("Chrono mismatch, expected: " + getId() + ", actual: " + other.getChronology().getId());
    }
    return other;
}
 
Example #8
Source File: Chronology.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Casts the {@code Temporal} to {@code ChronoLocalDateTime} with the same chronology.
 *
 * @param temporal   a date-time to cast, not null
 * @return the date-time checked and cast to {@code ChronoLocalDateTime}, not null
 * @throws ClassCastException if the date-time cannot be cast to ChronoLocalDateTimeImpl
 *  or the chronology is not equal this Chrono
 */
<D extends ChronoLocalDate> ChronoLocalDateTimeImpl<D> ensureChronoLocalDateTime(Temporal temporal) {
    @SuppressWarnings("unchecked")
    ChronoLocalDateTimeImpl<D> other = (ChronoLocalDateTimeImpl<D>) temporal;
    if (this.equals(other.toLocalDate().getChronology()) == false) {
        throw new ClassCastException("Chrono mismatch, required: " + getId()
                + ", supplied: " + other.toLocalDate().getChronology().getId());
    }
    return other;
}
 
Example #9
Source File: Chronology.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Casts the {@code Temporal} to {@code ChronoZonedDateTimeImpl} with the same chronology.
 *
 * @param temporal  a date-time to cast, not null
 * @return the date-time checked and cast to {@code ChronoZonedDateTimeImpl}, not null
 * @throws ClassCastException if the date-time cannot be cast to ChronoZonedDateTimeImpl
 *  or the chronology is not equal this Chrono
 */
<D extends ChronoLocalDate> ChronoZonedDateTimeImpl<D> ensureChronoZonedDateTime(Temporal temporal) {
    @SuppressWarnings("unchecked")
    ChronoZonedDateTimeImpl<D> other = (ChronoZonedDateTimeImpl<D>) temporal;
    if (this.equals(other.toLocalDate().getChronology()) == false) {
        throw new ClassCastException("Chrono mismatch, required: " + getId()
                + ", supplied: " + other.toLocalDate().getChronology().getId());
    }
    return other;
}
 
Example #10
Source File: TestOffsetDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_with_adjustment() {
    final OffsetDateTime sample = OffsetDateTime.of(LocalDate.of(2012, 3, 4), LocalTime.of(23, 5), OFFSET_PONE);
    TemporalAdjuster adjuster = new TemporalAdjuster() {
        @Override
        public Temporal adjustInto(Temporal dateTime) {
            return sample;
        }
    };
    assertEquals(TEST_2008_6_30_11_30_59_000000500.with(adjuster), sample);
}
 
Example #11
Source File: ChronoLocalDateTimeImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns a copy of this date-time with the new date and time, checking
 * to see if a new object is in fact required.
 *
 * @param newDate  the date of the new date-time, not null
 * @param newTime  the time of the new date-time, not null
 * @return the date-time, not null
 */
private ChronoLocalDateTimeImpl<D> with(Temporal newDate, LocalTime newTime) {
    if (date == newDate && time == newTime) {
        return this;
    }
    // Validate that the new DateTime is a ChronoLocalDate (and not something else)
    D cd = date.getChronology().ensureChronoLocalDate(newDate);
    return new ChronoLocalDateTimeImpl<D>(cd, newTime);
}
 
Example #12
Source File: ChronoLocalDateTimeImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    @SuppressWarnings("unchecked")
    ChronoLocalDateTime<D> end = (ChronoLocalDateTime<D>) toLocalDate().getChronology().localDateTime(endExclusive);
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        if (f.isTimeBased()) {
            long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY);
            switch (f) {
                case NANOS: amount = Jdk8Methods.safeMultiply(amount, NANOS_PER_DAY); break;
                case MICROS: amount = Jdk8Methods.safeMultiply(amount, MICROS_PER_DAY); break;
                case MILLIS: amount = Jdk8Methods.safeMultiply(amount, MILLIS_PER_DAY); break;
                case SECONDS: amount = Jdk8Methods.safeMultiply(amount, SECONDS_PER_DAY); break;
                case MINUTES: amount = Jdk8Methods.safeMultiply(amount, MINUTES_PER_DAY); break;
                case HOURS: amount = Jdk8Methods.safeMultiply(amount, HOURS_PER_DAY); break;
                case HALF_DAYS: amount = Jdk8Methods.safeMultiply(amount, 2); break;
            }
            return Jdk8Methods.safeAdd(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);
    }
    return unit.between(this, end);
}
 
Example #13
Source File: TestChronoLocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R adjustInto(R dateTime, long newValue) {
    return (R) this.dateTime;
}
 
Example #14
Source File: TestChronoLocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
FixedPeriodUnit(Temporal dateTime) {
    this.dateTime = dateTime;
}
 
Example #15
Source File: TestChronoLocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean isSupportedBy(Temporal dateTime) {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
Example #16
Source File: TestChronoZonedDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Temporal adjustInto(Temporal ignore) {
    return datetime;
}
 
Example #17
Source File: TestChronoLocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R addTo(R dateTime, long periodToAdd) {
    return (R) this.dateTime;
}
 
Example #18
Source File: TestChronoLocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public long between(Temporal temporal1, Temporal temporal2) {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
Example #19
Source File: TestChronoLocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
FixedDateTimeField(Temporal dateTime) {
    this.dateTime = dateTime;
}
 
Example #20
Source File: TestChronoZonedDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Temporal addTo(Temporal ignore) {
    return datetime;
}
 
Example #21
Source File: TestChronoLocalDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Temporal adjustInto(Temporal ignore) {
    return datetime;
}
 
Example #22
Source File: TestChronoZonedDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
FixedAdjuster(Temporal datetime) {
    this.datetime = datetime;
}
 
Example #23
Source File: TestChronoLocalDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R adjustInto(R dateTime, long newValue) {
    return (R) this.dateTime;
}
 
Example #24
Source File: TestDayOfWeek.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_adjustInto_null() {
    DayOfWeek.MONDAY.adjustInto((Temporal) null);
}
 
Example #25
Source File: TestLocalTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public long between(Temporal r, Temporal r2) {
    throw new UnsupportedOperationException();
}
 
Example #26
Source File: TestLocalTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public <R extends Temporal> R addTo(R r, long l) {
    throw new UnsupportedOperationException();
}
 
Example #27
Source File: TestLocalTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean isSupportedBy(Temporal temporal) {
    return false;
}
 
Example #28
Source File: TestLocalTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public long between(Temporal r, Temporal r2) {
    throw new UnsupportedOperationException();
}
 
Example #29
Source File: TestLocalTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public <R extends Temporal> R addTo(R r, long l) {
    throw new UnsupportedOperationException();
}
 
Example #30
Source File: TestLocalTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean isSupportedBy(Temporal temporal) {
    return false;
}