Java : TemporalAdjusters con ejemplos
TemporalAdjusters (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de TemporalAdjusters.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
final var adjuster = TemporalAdjusters.lastDayOfMonth();
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-01-31
final var date2 = LocalDate.of(2100, 2, 15);
System.out.println(date2); // 2100-02-15
System.out.println(date2.with(adjuster)); // 2100-02-28
final var dateTime = LocalDateTime.of(2100, 4, 2, 12, 30);
System.out.println(dateTime); // 2100-04-02T12:30
System.out.println(dateTime.with(adjuster)); // 2100-04-30T12:30
Methods
static TemporalAdjuster dayOfWeekInMonth (int ordinal, DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
final var date = LocalDate.of(2100, 12, 1);
System.out.println(date.format(formatter)); // Wednesday, December 1, 2100
{
final var ret1 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.WEDNESDAY));
System.out.println(ret1.format(formatter)); // Wednesday, December 1, 2100
final var ret2 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.THURSDAY));
System.out.println(ret2.format(formatter)); // Thursday, December 2, 2100
final var ret3 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.FRIDAY));
System.out.println(ret3.format(formatter)); // Friday, December 3, 2100
}
{
final var ret1 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SUNDAY));
System.out.println(ret1.format(formatter)); // Sunday, December 5, 2100
final var ret2 = date.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SUNDAY));
System.out.println(ret2.format(formatter)); // Sunday, December 12, 2100
final var ret3 = date.with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.SUNDAY));
System.out.println(ret3.format(formatter)); // Sunday, December 19, 2100
}
static TemporalAdjuster firstDayOfMonth ()
final var adjuster = TemporalAdjusters.firstDayOfMonth();
final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2100-01-01
final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2100-02-01
final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 1999-12-01
static TemporalAdjuster firstDayOfNextMonth ()
final var adjuster = TemporalAdjusters.firstDayOfNextMonth();
final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2100-02-01
final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2100-03-01
final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 2000-01-01
static TemporalAdjuster firstDayOfNextYear ()
final var adjuster = TemporalAdjusters.firstDayOfNextYear();
final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2101-01-01
final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2101-01-01
final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 2000-01-01
static TemporalAdjuster firstDayOfYear ()
final var adjuster = TemporalAdjusters.firstDayOfYear();
final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2100-01-01
final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2100-01-01
final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 1999-01-01
static TemporalAdjuster firstInMonth (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
final var date = LocalDate.of(2100, 12, 1);
System.out.println(date.format(formatter)); // Wednesday, December 1, 2100
final var ret1 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 6, 2100
final var ret2 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 7, 2100
final var ret3 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 1, 2100
final var ret4 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 2, 2100
final var ret5 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 3, 2100
static TemporalAdjuster lastDayOfMonth ()
final var adjuster = TemporalAdjusters.lastDayOfMonth();
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-01-31
final var date2 = LocalDate.of(2100, 2, 15);
System.out.println(date2); // 2100-02-15
System.out.println(date2.with(adjuster)); // 2100-02-28
final var date3 = LocalDate.of(1999, 4, 3);
System.out.println(date3); // 1999-04-03
System.out.println(date3.with(adjuster)); // 1999-04-30
static TemporalAdjuster lastDayOfYear ()
final var adjuster = TemporalAdjusters.lastDayOfYear();
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-12-31
final var date2 = LocalDate.of(2100, 2, 15);
System.out.println(date2); // 2100-02-15
System.out.println(date2.with(adjuster)); // 2100-12-31
final var date3 = LocalDate.of(1999, 12, 3);
System.out.println(date3); // 1999-12-03
System.out.println(date3.with(adjuster)); // 1999-12-31
static TemporalAdjuster lastInMonth (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
final var date = LocalDate.of(2100, 6, 1);
System.out.println(date.format(formatter)); // Tuesday, June 1, 2100
final var ret1 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, June 28, 2100
final var ret2 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, June 29, 2100
final var ret3 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, June 30, 2100
final var ret4 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, June 24, 2100
final var ret5 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, June 25, 2100
static TemporalAdjuster next (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // Wednesday, December 15, 2100
final var ret1 = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 20, 2100
final var ret2 = date.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 21, 2100
final var ret3 = date.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 22, 2100
final var ret4 = date.with(TemporalAdjusters.next(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 16, 2100
final var ret5 = date.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 17, 2100
static TemporalAdjuster nextOrSame (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // Wednesday, December 15, 2100
final var ret1 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 20, 2100
final var ret2 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 21, 2100
final var ret3 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 15, 2100
final var ret4 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 16, 2100
final var ret5 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 17, 2100
static TemporalAdjuster ofDateAdjuster (UnaryOperator<LocalDate> dateBasedAdjuster)
final var adjuster = TemporalAdjusters.ofDateAdjuster(date -> {
return date.plusDays(2);
});
final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-01-03
final var date2 = LocalDate.of(1999, 12, 31);
System.out.println(date2); // 1999-12-31
System.out.println(date2.with(adjuster)); // 2000-01-02
static TemporalAdjuster previous (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // Wednesday, December 15, 2100
final var ret1 = date.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 13, 2100
final var ret2 = date.with(TemporalAdjusters.previous(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 14, 2100
final var ret3 = date.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 8, 2100
final var ret4 = date.with(TemporalAdjusters.previous(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 9, 2100
final var ret5 = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 10, 2100
static TemporalAdjuster previousOrSame (DayOfWeek dayOfWeek)
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // Wednesday, December 15, 2100
final var ret1 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 13, 2100
final var ret2 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 14, 2100
final var ret3 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 15, 2100
final var ret4 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 9, 2100
final var ret5 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 10, 2100
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset