下面列出了怎么用java.time.temporal.TemporalAdjuster的API类实例代码及写法,或者点击链接到github查看源代码。
@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");
}
}
}
@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");
}
}
}
@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");
}
}
}
@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");
}
}
}
@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");
}
}
}
@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");
}
}
}
public static LocalDate getDayAfterDays(LocalDate startDate, int days) {
if (startDate == null) {
throw new IllegalArgumentException("Start date cannot be null");
}
Period period = Period.ofDays(days);
TemporalAdjuster ta = p -> p.plus(period);
LocalDate endDate = startDate.with(ta);
return endDate;
}
@Test
@Tag("TODO")
@Order(2)
public void verifyStreamOperationsOnTemporalAdjustment() {
TemporalAdjuster nextOrSameSunday = temporal -> {
LocalDate localDate = LocalDate.from(temporal);
// TODO: Add a localDate manipulation that returns
// either the next sunday after current date
// it is a or current date, if Sunday.
// Check: java.time.LocalDate.with(java.time.temporal.TemporalAdjuster)
// Check: java.time.temporal.TemporalAdjusters.nextOrSame(DayOfWeek)
// HINT: localDate = localDate.???(TemporalAdjusters.nextOrSame(???));
return temporal.with(localDate);
};
Stream<LocalDate> someDates = Stream.of(
LocalDate.of(1997, 8, 29),
LocalDate.of(2015, 1, 1),
LocalDate.of(2015, 1, 4));
// TODO: Fix the list mapping below.
// Check: java.time.LocalDate.with(java.time.temporal.TemporalAdjuster)
List<LocalDate> collectSundays = someDates
.map(each -> each)
.collect(Collectors.toList());
assertEquals("1997-08-31",
collectSundays.get(0).toString(),
"Sunday on or after 1997-08-29 is 1997-08-31");
assertEquals("2015-01-04",
collectSundays.get(1).toString(),
"Sunday on or after 2015-01-01 is 2015-01-04");
assertEquals("2015-01-04",
collectSundays.get(2).toString(),
"Sunday on or after 1997-01-04 is 2015-01-04");
}
@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));
}
@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));
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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));
}
@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);
}
@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));
}
@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);
}
@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);
}
@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);
}
@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);
}
/**
* Returns an adjusted copy of this date-time.
* <p>
* This returns a {@code ZonedDateTime}, based on this one, with the date-time adjusted.
* The adjustment takes place using the specified adjuster strategy object.
* Read the documentation of the adjuster to understand what adjustment will be made.
* <p>
* A simple adjuster might simply set the one of the fields, such as the year field.
* A more complex adjuster might set the date to the last day of the month.
* A selection of common adjustments is provided in
* {@link java.time.temporal.TemporalAdjusters TemporalAdjusters}.
* These include finding the "last day of the month" and "next Wednesday".
* Key date-time classes also implement the {@code TemporalAdjuster} interface,
* such as {@link Month} and {@link java.time.MonthDay MonthDay}.
* The adjuster is responsible for handling special cases, such as the varying
* lengths of month and leap years.
* <p>
* For example this code returns a date on the last day of July:
* <pre>
* import static java.time.Month.*;
* import static java.time.temporal.TemporalAdjusters.*;
*
* result = zonedDateTime.with(JULY).with(lastDayOfMonth());
* </pre>
* <p>
* The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
* thus this method can be used to change the date, time or offset:
* <pre>
* result = zonedDateTime.with(date);
* result = zonedDateTime.with(time);
* </pre>
* <p>
* {@link ZoneOffset} also implements {@code TemporalAdjuster} however using it
* as an argument typically has no effect. The offset of a {@code ZonedDateTime} is
* controlled primarily by the time-zone. As such, changing the offset does not generally
* make sense, because there is only one valid offset for the local date-time and zone.
* If the zoned date-time is in a daylight savings overlap, then the offset is used
* to switch between the two valid offsets. In all other cases, the offset is ignored.
* <p>
* The result of this method is obtained by invoking the
* {@link TemporalAdjuster#adjustInto(Temporal)} method on the
* specified adjuster passing {@code this} as the argument.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param adjuster the adjuster to use, not null
* @return a {@code ZonedDateTime} based on {@code this} with the adjustment made, not null
* @throws DateTimeException if the adjustment cannot be made
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public ZonedDateTime with(TemporalAdjuster adjuster) {
// optimizations
if (adjuster instanceof LocalDate) {
return resolveLocal(LocalDateTime.of((LocalDate) adjuster, dateTime.toLocalTime()));
} else if (adjuster instanceof LocalTime) {
return resolveLocal(LocalDateTime.of(dateTime.toLocalDate(), (LocalTime) adjuster));
} else if (adjuster instanceof LocalDateTime) {
return resolveLocal((LocalDateTime) adjuster);
} else if (adjuster instanceof OffsetDateTime) {
OffsetDateTime odt = (OffsetDateTime) adjuster;
return ofLocal(odt.toLocalDateTime(), zone, odt.getOffset());
} else if (adjuster instanceof Instant) {
Instant instant = (Instant) adjuster;
return create(instant.getEpochSecond(), instant.getNano(), zone);
} else if (adjuster instanceof ZoneOffset) {
return resolveOffset((ZoneOffset) adjuster);
}
return (ZonedDateTime) adjuster.adjustInto(this);
}
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjustment_null() {
TEST_12_30_40_987654321.with((TemporalAdjuster) null);
}
@Override
@SuppressWarnings("unchecked")
public D with(TemporalAdjuster adjuster) {
return (D) ChronoLocalDate.super.with(adjuster);
}
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjustment_null() {
TEST_11_30_59_500_PONE.with((TemporalAdjuster) null);
}
@Override
@SuppressWarnings("unchecked")
public D with(TemporalAdjuster adjuster) {
return (D) ChronoLocalDate.super.with(adjuster);
}
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjuster_null() {
ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
base.with((TemporalAdjuster) null);
}
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjustment_null() {
TEST_2008_6_30_11_30_59_000000500.with((TemporalAdjuster) null);
}
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjustment_null() {
TEST_2007_07_15_12_30_40_987654321.with((TemporalAdjuster) null);
}