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 :


Summary

Ajustadores temporales comunes y útiles. (Traducción automática)

Class diagram

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)

Devuelve el ajustador del día de la semana en el mes, que devuelve una nueva fecha con el día de la semana ordinal basado en el mes. (Traducción automática)

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 ()

Devuelve el ajustador "primer día del mes", que devuelve una nueva fecha establecida en el primer día del mes actual. (Traducción automática)

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 ()

Devuelve el ajustador "primer día del mes siguiente", que devuelve una nueva fecha establecida en el primer día del mes siguiente. (Traducción automática)

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 ()

Devuelve el ajustador "primer día del próximo año", que devuelve una nueva fecha establecida en el primer día del próximo año. (Traducción automática)

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 ()

Devuelve el ajustador "primer día del año", que devuelve una nueva fecha establecida en el primer día del año actual. (Traducción automática)

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)

Devuelve el primer ajustador del mes, que devuelve una nueva fecha en el mismo mes con el primer día de la semana coincidente. (Traducción automática)

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 ()

Devuelve el ajustador "último día del mes", que devuelve una nueva fecha establecida en el último día del mes actual. (Traducción automática)

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 ()

Devuelve el ajustador "último día del año", que devuelve una nueva fecha establecida en el último día del año actual. (Traducción automática)

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)

Devuelve el último ajustador del mes, que devuelve una nueva fecha en el mismo mes con el último día de la semana coincidente. (Traducción automática)

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)

Devuelve el siguiente ajustador del día de la semana, que ajusta la fecha a la primera ocurrencia del día de la semana especificado después de la fecha que se está ajustando. (Traducción automática)

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)

Devuelve el ajustador del siguiente o mismo día de la semana, que ajusta la fecha a la primera ocurrencia del día de la semana especificado después de la fecha que se está ajustando, a menos que ya sea en ese día, en cuyo caso se devuelve el mismo objeto. (Traducción automática)

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)

Obtiene un TemporalAdjuster que envuelve un ajustador de fecha. (Traducción automática)

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)

Devuelve el ajustador del día de la semana anterior, que ajusta la fecha a la primera ocurrencia del día de la semana especificado antes de la fecha que se está ajustando. (Traducción automática)

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)

Devuelve el ajustador del día de la semana anterior o igual, que ajusta la fecha a la primera ocurrencia del día de la semana especificado antes de la fecha que se está ajustando, a menos que ya sea en ese día, en cuyo caso se devuelve el mismo objeto. (Traducción automática)

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

To top of page