本文整理汇总了Java中java.time.temporal.Temporal类的典型用法代码示例。如果您正苦于以下问题:Java Temporal类的具体用法?Java Temporal怎么用?Java Temporal使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Temporal类属于java.time.temporal包,在下文中一共展示了Temporal类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: renderTemporal
import java.time.temporal.Temporal; //导入依赖的package包/类
/**
* Renders a Temporal value type Field
* @param property Property to render
* @return Field instance
*/
@SuppressWarnings("unchecked")
protected Field<T> renderTemporal(Property<T> property) {
TemporalInputBuilder builder = null;
if (LocalDate.class.isAssignableFrom(property.getType())) {
builder = input.localDate(false);
} else if (LocalDateTime.class.isAssignableFrom(property.getType())) {
builder = input.localDateTime(false);
} else {
throw new UnsupportedTemporalTypeException(
"Temporal type " + property.getType().getName() + " is not supported by default field renderer");
}
final TemporalInputBuilder<Temporal, ?> b = builder;
// set locale from LocalizationContext, if any
LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap((c) -> c.getLocale())
.ifPresent((l) -> b.locale(l));
return postProcessField(b.asField(), property);
}
示例2: addTo
import java.time.temporal.Temporal; //导入依赖的package包/类
@Override
public Temporal addTo(Temporal temporal) {
validateChrono(temporal);
if (months == 0) {
if (years != 0) {
temporal = temporal.plus(years, YEARS);
}
} else {
long monthRange = monthRange();
if (monthRange > 0) {
temporal = temporal.plus(years * monthRange + months, MONTHS);
} else {
if (years != 0) {
temporal = temporal.plus(years, YEARS);
}
temporal = temporal.plus(months, MONTHS);
}
}
if (days != 0) {
temporal = temporal.plus(days, DAYS);
}
return temporal;
}
示例3: ensureValid
import java.time.temporal.Temporal; //导入依赖的package包/类
/**
* Casts the {@code Temporal} to {@code ChronoLocalDateTime} ensuring it bas the specified chronology.
*
* @param chrono the chronology to check for, not null
* @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 Chronology
*/
static <R extends ChronoLocalDate> ChronoLocalDateTimeImpl<R> ensureValid(Chronology chrono, Temporal temporal) {
@SuppressWarnings("unchecked")
ChronoLocalDateTimeImpl<R> other = (ChronoLocalDateTimeImpl<R>) temporal;
if (chrono.equals(other.getChronology()) == false) {
throw new ClassCastException("Chronology mismatch, required: " + chrono.getId()
+ ", actual: " + other.getChronology().getId());
}
return other;
}
示例4: test_with_adjustment
import java.time.temporal.Temporal; //导入依赖的package包/类
@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);
}
示例5: test_with_adjustment
import java.time.temporal.Temporal; //导入依赖的package包/类
@Test
public void test_with_adjustment() {
final OffsetTime sample = OffsetTime.of(23, 5, 0, 0, OFFSET_PONE);
TemporalAdjuster adjuster = new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal dateTime) {
return sample;
}
};
assertEquals(TEST_11_30_59_500_PONE.with(adjuster), sample);
}
示例6: until
import java.time.temporal.Temporal; //导入依赖的package包/类
@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);
}
示例7: get
import java.time.temporal.Temporal; //导入依赖的package包/类
@Override
default int get(TemporalField field) {
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
case INSTANT_SECONDS:
throw new UnsupportedTemporalTypeException("Invalid field 'InstantSeconds' for get() method, use getLong() instead");
case OFFSET_SECONDS:
return getOffset().getTotalSeconds();
}
return toLocalDateTime().get(field);
}
return Temporal.super.get(field);
}
示例8: test_adjustDate
import java.time.temporal.Temporal; //导入依赖的package包/类
@Test
public void test_adjustDate() {
LocalDate base = LocalDate.of(2007, 2, 12);
for (int i = -4; i <= 2104; i++) {
Temporal result = Year.of(i).adjustInto(base);
assertEquals(result, LocalDate.of(i, 2, 12));
}
}
示例9: test_formatStyle
import java.time.temporal.Temporal; //导入依赖的package包/类
@Test(dataProvider = "formatStyle")
public void test_formatStyle(Temporal temporal, FormatStyle style, String formattedStr) {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendLocalized(style, style).appendLiteral(" ").appendZoneOrOffsetId().toFormatter();
formatter = formatter.withLocale(Locale.US);
assertEquals(formatter.format(temporal), formattedStr);
}
示例10: test_with_TemporalAdjuster
import java.time.temporal.Temporal; //导入依赖的package包/类
@Test
public void test_with_TemporalAdjuster() {
Year base = Year.of(-10);
for (int i = -4; i <= 2104; i++) {
Temporal result = base.with(Year.of(i));
assertEquals(result, Year.of(i));
}
}
示例11: test_with_adjustment
import java.time.temporal.Temporal; //导入依赖的package包/类
@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);
}
示例12: test_subtractFrom
import java.time.temporal.Temporal; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_subtractFrom(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoLocalDate date = chrono.dateNow();
Temporal result = period.subtractFrom(date);
assertEquals(result, date.minus(14, MONTHS).minus(3, DAYS));
}
示例13: until
import java.time.temporal.Temporal; //导入依赖的package包/类
@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);
}
示例14: invokeSpecial0
import java.time.temporal.Temporal; //导入依赖的package包/类
@Override
protected <T> T invokeSpecial0(@NotNull SNode node, @NotNull SMethod<T> method, @Nullable Object[] parameters) {
int methodIndex = BH_METHODS.indexOf(method);
if (methodIndex < 0) {
throw new BHMethodNotFoundException(this, method);
}
switch (methodIndex) {
case 0:
return (T) ((Temporal) GeefTemporeleWaarde_id5kuxuwXEUJM(node));
case 1:
return (T) ((String) GeefWaardeString_idFzw$g_H4hz(node));
default:
throw new BHMethodNotFoundException(this, method);
}
}
示例15: ensureValid
import java.time.temporal.Temporal; //导入依赖的package包/类
/**
* Casts the {@code Temporal} to {@code ChronoZonedDateTimeImpl} ensuring it bas the specified chronology.
*
* @param chrono the chronology to check for, not null
* @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 Chronology
*/
static <R extends ChronoLocalDate> ChronoZonedDateTimeImpl<R> ensureValid(Chronology chrono, Temporal temporal) {
@SuppressWarnings("unchecked")
ChronoZonedDateTimeImpl<R> other = (ChronoZonedDateTimeImpl<R>) temporal;
if (chrono.equals(other.getChronology()) == false) {
throw new ClassCastException("Chronology mismatch, required: " + chrono.getId()
+ ", actual: " + other.getChronology().getId());
}
return other;
}