本文整理汇总了Java中java.time.temporal.TemporalAdjuster类的典型用法代码示例。如果您正苦于以下问题:Java TemporalAdjuster类的具体用法?Java TemporalAdjuster怎么用?Java TemporalAdjuster使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TemporalAdjuster类属于java.time.temporal包,在下文中一共展示了TemporalAdjuster类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_badWithAdjusterChrono
import java.time.temporal.TemporalAdjuster; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_badWithAdjusterChrono(Chronology chrono) {
LocalDate refDate = LocalDate.of(2013, 1, 1);
ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
for (Chronology[] clist : data_of_calendars()) {
Chronology chrono2 = clist[0];
ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
TemporalAdjuster adjuster = new FixedAdjuster(czdt2);
if (chrono != chrono2) {
try {
czdt.with(adjuster);
Assert.fail("WithAdjuster should have thrown a ClassCastException, "
+ "required: " + czdt + ", supplied: " + czdt2);
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
ChronoZonedDateTime<?> result = czdt.with(adjuster);
assertEquals(result, czdt2, "WithAdjuster failed to replace date");
}
}
}
示例2: test_badWithAdjusterChrono
import java.time.temporal.TemporalAdjuster; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_badWithAdjusterChrono(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);
TemporalAdjuster adjuster = new FixedAdjuster(date2);
if (chrono != chrono2) {
try {
date.with(adjuster);
Assert.fail("WithAdjuster should have thrown a ClassCastException");
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDate result = date.with(adjuster);
assertEquals(result, date2, "WithAdjuster failed to replace date");
}
}
}
示例3: test_badWithAdjusterChrono
import java.time.temporal.TemporalAdjuster; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_badWithAdjusterChrono(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);
TemporalAdjuster adjuster = new FixedAdjuster(cdt2);
if (chrono != chrono2) {
try {
cdt.with(adjuster);
Assert.fail("WithAdjuster should have thrown a ClassCastException, "
+ "required: " + cdt + ", supplied: " + cdt2);
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDateTime<?> result = cdt.with(adjuster);
assertEquals(result, cdt2, "WithAdjuster failed to replace date");
}
}
}
示例4: findSpecificTimes
import java.time.temporal.TemporalAdjuster; //导入依赖的package包/类
public static void findSpecificTimes() {
//STATIC DAYS
MonthDay zachBirthday = MonthDay.of(Month.MARCH, 15);
MonthDay julyFourth = MonthDay.of(Month.JULY, 4);
LocalDate zachBDay2016 = zachBirthday.atYear(2016);
//DYNAMIC DAYS - Find the date for the next Thanksgiving
TemporalAdjuster fourthThursdayAdjuster =
TemporalAdjusters.dayOfWeekInMonth(4, THURSDAY);
LocalDate thanksgiving = LocalDate.now().with(Month.NOVEMBER).with(fourthThursdayAdjuster);
if (LocalDate.now().isAfter(thanksgiving)) {
thanksgiving =
thanksgiving.plusYears(1).with(fourthThursdayAdjuster);
}
//DYNAMIC DAYS WITH LAMBDA BASED INTERFACES
LocalDate thanksgivingEasy = LocalDate.now().with(nextThanksgiving());
}
示例5: nextThanksgiving
import java.time.temporal.TemporalAdjuster; //导入依赖的package包/类
public static TemporalAdjuster nextThanksgiving() {
return (temporal -> {
if (temporal.isSupported(YEAR) && temporal.isSupported(MONTH_OF_YEAR) && temporal
.isSupported(DAY_OF_MONTH)) {
TemporalAdjuster fourthThursdayAdjuster = TemporalAdjusters.dayOfWeekInMonth(4, THURSDAY);
Temporal thanksgiving = temporal.with(NOVEMBER).with(fourthThursdayAdjuster);
if (LocalDate.from(temporal).isAfter(LocalDate.from(thanksgiving)) || temporal.equals(thanksgiving)) {
thanksgiving = thanksgiving.plus(Period.ofYears(1)).with(fourthThursdayAdjuster);
}
return thanksgiving;
}
throw new UnsupportedTemporalTypeException(
"Temporal must support Year, Month, and Day");
});
}
示例6: with
import java.time.temporal.TemporalAdjuster; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public ChronoLocalDateTimeImpl<D> with(TemporalAdjuster adjuster) {
if (adjuster instanceof ChronoLocalDate) {
// The Chronology is checked in with(date,time)
return with((ChronoLocalDate) adjuster, time);
} else if (adjuster instanceof LocalTime) {
return with(date, (LocalTime) adjuster);
} else if (adjuster instanceof ChronoLocalDateTimeImpl) {
return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), (ChronoLocalDateTimeImpl<?>) adjuster);
}
return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), (ChronoLocalDateTimeImpl<?>) adjuster.adjustInto(this));
}
示例7: test_with_adjustment
import java.time.temporal.TemporalAdjuster; //导入依赖的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);
}
示例8: test_with_adjustment
import java.time.temporal.TemporalAdjuster; //导入依赖的package包/类
@Test
public void test_with_adjustment() {
final LocalDateTime sample = LocalDateTime.of(2012, 3, 4, 23, 5);
TemporalAdjuster adjuster = new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal dateTime) {
return sample;
}
};
assertEquals(TEST_2007_07_15_12_30_40_987654321.with(adjuster), sample);
}
示例9: test_with_adjustment
import java.time.temporal.TemporalAdjuster; //导入依赖的package包/类
@Test
public void test_with_adjustment() {
final LocalDate sample = LocalDate.of(2012, 3, 4);
TemporalAdjuster adjuster = new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal dateTime) {
return sample;
}
};
assertEquals(TEST_2007_07_15.with(adjuster), sample);
}
示例10: test_with_adjustment
import java.time.temporal.TemporalAdjuster; //导入依赖的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);
}
示例11: test_with_adjustment
import java.time.temporal.TemporalAdjuster; //导入依赖的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);
}
示例12: test_with_adjustment_AmPm
import java.time.temporal.TemporalAdjuster; //导入依赖的package包/类
@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(23, 30, 59, 500, OFFSET_PONE));
}