devstory

Le Tutoriel de Java TemporalAdjusters

  1. TemporalAdjusters
  2. TemporalAdjusters Methods
  3. ofDateAdjuster(UnaryOperator<LocalDate>)
  4. firstDayOfMonth()
  5. lastDayOfMonth()
  6. firstDayOfNextMonth()
  7. firstDayOfYear()
  8. lastDayOfYear()
  9. firstDayOfNextYear()
  10. firstInMonth(DayOfWeek)
  11. lastInMonth(DayOfWeek)
  12. dayOfWeekInMonth(int, DayOfWeek)
  13. next(DayOfWeek)
  14. nextOrSame(DayOfWeek)
  15. previous(DayOfWeek)
  16. previousOrSame(DayOfWeek)

1. TemporalAdjusters

La classe TemporalAdjusters fournit des méthodes statiques pour obtenir des objets TemporalAdjuster utiles et courants, tels que :
  • Trouver le premier ou le dernier jour du mois.
  • Trouver le premier jour du mois prochain.
  • Trouver le premier ou le dernier jour de l'année.
  • Trouver le premier jour de l'année prochaine.
  • Recherche du premier ou du dernier "jour de la semaine" dans un mois, par exemple "premier mercredi de juin".
  • Recherche du "jour de la semaine" suivant ou précédent, tel que "jeudi prochain".
Voir aussi l'article sur l'interface TemporalAdjuster avec des exemples de base et TemporalAdjuster personnalisé :

2. TemporalAdjusters Methods

Toutes les méthodes fournies par TemporalAdjusters sont statiques et renvoient un objet TemporalAdjuster.
public static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster)

public static TemporalAdjuster firstDayOfMonth()  
public static TemporalAdjuster lastDayOfMonth()  

public static TemporalAdjuster firstDayOfNextMonth()  

public static TemporalAdjuster firstDayOfYear()  
public static TemporalAdjuster lastDayOfYear()  

public static TemporalAdjuster firstDayOfNextYear()

public static TemporalAdjuster firstInMonth(DayOfWeek dayOfWeek)  
public static TemporalAdjuster lastInMonth(DayOfWeek dayOfWeek)  

public static TemporalAdjuster dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek)

public static TemporalAdjuster next(DayOfWeek dayOfWeek)   
public static TemporalAdjuster nextOrSame(DayOfWeek dayOfWeek)  

public static TemporalAdjuster previous(DayOfWeek dayOfWeek)   
public static TemporalAdjuster previousOrSame(DayOfWeek dayOfWeek)

3. ofDateAdjuster(UnaryOperator<LocalDate>)

Cette méthode renvoie un objet TemporalAdjuster pour ajuster la partie LocalDate d'un objet Temporal. Les autres parties de l'objet Temporal restent inchangées.
public static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster) {
    Objects.requireNonNull(dateBasedAdjuster, "dateBasedAdjuster");
    return (temporal) -> {
        LocalDate input = LocalDate.from(temporal);
        LocalDate output = dateBasedAdjuster.apply(input);
        return temporal.with(output);
    };
}
Par exemple, ajouter 2 jours à un objet Temporal :
Temporal
Example
After 2 days
ZonedDateTime
2020-05-25T13:30:59+6[Asia/Bishkek]
2020-05-27T13:30:59+6[Asia/Bishkek]
OffsetDateTime
2020-05-25T13:30:59+6
2020-05-27T13:30:59+6
LocalDateTime
2020-05-25T13:30:59
2020-05-27T13:30:59
Par exemple : Un TemporalAdjuster pour ajouter 2 jours.
TemporalAdjusters_ofDateAdjuster_ex1.java
package org.o7planning.temporaladjusters.ex;

import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

public class TemporalAdjusters_ofDateAdjuster_ex1 {
    public static void main(String[] args) {
        TemporalAdjuster add2DaysAdjuster = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(2));
        
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        ZonedDateTime after2Days = zonedDateTime.with(add2DaysAdjuster);
        
        System.out.println("zonedDateTime: " + zonedDateTime);
        System.out.println("after2Days: " + after2Days);
    }
}
Output:
zonedDateTime: 2021-07-05T18:53:19.138987+06:00[Asia/Bishkek]
after2Days: 2021-07-07T18:53:19.138987+06:00[Asia/Bishkek]
  • Le Tutoriel de Java UnaryOperator

4. firstDayOfMonth()

Renvoyer un TemporalAdjuster - "premier jour du mois".
public static TemporalAdjuster firstDayOfMonth()
Par exemple :
TemporalAdjusters_firstDayOfMonth_ex1.java
LocalDate localDate = LocalDate.now();
System.out.println("today is: " + localDate);

LocalDate firstDayOfMonth = (LocalDate) TemporalAdjusters.firstDayOfMonth().adjustInto(localDate);
System.out.println("firstDayOfMonth: " + firstDayOfMonth);

DayOfWeek dayOfWeek = firstDayOfMonth.getDayOfWeek();
System.out.println(" > day of week: " + dayOfWeek);
Output:
today is: 2021-07-05
firstDayOfMonth: 2021-07-01
 > day of week: THURSDAY
  • Le Tutoriel de Java DayOfWeek

5. lastDayOfMonth()

Renvoyer un TemporalAdjuster - "dernier jour du mois".
public static TemporalAdjuster lastDayOfMonth()
Par exemple :
TemporalAdjusters_lastDayOfMonth_ex1.java
LocalDate localDate = LocalDate.now();
System.out.println("today is: " + localDate);

LocalDate lastDayOfMonth = (LocalDate) TemporalAdjusters.lastDayOfMonth().adjustInto(localDate);
System.out.println("lastDayOfMonth: " + lastDayOfMonth);

DayOfWeek dayOfWeek = lastDayOfMonth.getDayOfWeek();
System.out.println(" > day of week: " + dayOfWeek);
Output:
today is: 2021-07-05
lastDayOfMonth: 2021-07-31
 > day of week: SATURDAY
  • Le Tutoriel de Java DayOfWeek

6. firstDayOfNextMonth()

Renvoyer un TemporalAdjuster - "premier jour du mois prochain".
public static TemporalAdjuster firstDayOfNextMonth()
Par exemple :
TemporalAdjusters_firstDayOfNextMonth_ex1.java
LocalDate localDate = LocalDate.now();
System.out.println("today is: " + localDate);

LocalDate firstDayOfNextMonth = (LocalDate) localDate.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println("firstDayOfNextMonth: " + firstDayOfNextMonth);

DayOfWeek dayOfWeek = firstDayOfNextMonth.getDayOfWeek();
System.out.println(" > day of week: " + dayOfWeek);

int dayOfMonth = firstDayOfNextMonth.getDayOfMonth();
System.out.println(" > day of month: " + dayOfMonth);
Output:
today is: 2021-07-05
firstDayOfNextMonth: 2021-08-01
 > day of week: SUNDAY
 > day of month: 1

7. firstDayOfYear()

Renvoyer un TemporalAdjuster - "premier jour de l'année".
public static TemporalAdjuster firstDayOfYear()
Par exemple :
TemporalAdjusters_firstDayOfYear_ex1.java
LocalDate localDate = LocalDate.now();
System.out.println("today is: " + localDate);

LocalDate firstDayOfYear = (LocalDate) localDate.with(TemporalAdjusters.firstDayOfYear());
System.out.println("firstDayOfYear: " + firstDayOfYear);

DayOfWeek dayOfWeek = firstDayOfYear.getDayOfWeek();
System.out.println(" > day of week: " + dayOfWeek);
Output:
today is: 2021-07-05
firstDayOfYear: 2021-01-01
 > day of week: FRIDAY

8. lastDayOfYear()

Renvoyer un TemporalAdjuster - "dernier jour de l'année".
public static TemporalAdjuster lastDayOfYear()
Par exemple :
TemporalAdjusters_lastDayOfYear_ex1.java
LocalDate localDate = LocalDate.now();
System.out.println("today is: " + localDate);

LocalDate lastDayOfYear = (LocalDate) localDate.with(TemporalAdjusters.lastDayOfYear());
System.out.println("lastDayOfYear: " + lastDayOfYear);

DayOfWeek dayOfWeek = lastDayOfYear.getDayOfWeek();
System.out.println(" > day of week: " + dayOfWeek);
Output:
today is: 2021-07-05
lastDayOfYear: 2021-12-31
 > day of week: FRIDAY

9. firstDayOfNextYear()

Renvoyer un TemporalAdjuster - "premier jour de l'année prochaine".
public static TemporalAdjuster firstDayOfNextYear()
Par exemple:
TemporalAdjusters_firstDayOfNextYear_ex1.java
LocalDate localDate = LocalDate.now();
System.out.println("today is: " + localDate);

LocalDate firstDayOfNextYear = (LocalDate) localDate.with(TemporalAdjusters.firstDayOfNextYear());
System.out.println("firstDayOfNextYear: " + firstDayOfNextYear);

DayOfWeek dayOfWeek = firstDayOfNextYear.getDayOfWeek();
System.out.println(" > day of week: " + dayOfWeek);

int dayOfYear = firstDayOfNextYear.getDayOfYear();
System.out.println(" > day of year: " + dayOfYear);
Output:
today is: 2021-07-05
firstDayOfNextYear: 2022-01-01
> day of week: SATURDAY
> day of year: 1

10. firstInMonth(DayOfWeek)

Renvoyer un TemporalAdjuster - "Premier jour de la semaine du mois". Par exemple, rechercher le premier MONDAY du mois.
public static TemporalAdjuster firstInMonth(DayOfWeek dayOfWeek)
Actuellement aux États-Unis, la date de début de l'heure d'été DST est le deuxième dimanche de mars et la fin est le premier dimanche de novembre. La question est de savoir quelle est la date de début de l'heure d'été DST 2025 aux États-Unis ?
TemporalAdjusters_firstInMonth_ex1.java
// Find the first SUNDAY in month.
TemporalAdjuster adjuster1 = TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY);
// Find the next SUNDAY
TemporalAdjuster adjuster2 = TemporalAdjusters.next(DayOfWeek.SUNDAY);

LocalDate localDate = LocalDate.of(2025, 3, 1); // 2025-03-01

LocalDate firstSundayInMonth = (LocalDate) localDate.with(adjuster1);
LocalDate secondSundayInMonth = (LocalDate) firstSundayInMonth.with(adjuster2);

System.out.println("firstSaturdayInMonth: " + firstSundayInMonth); // 2025-03-02
System.out.println("secondSundayInMonth: " + secondSundayInMonth);  // 2025-03-09

11. lastInMonth(DayOfWeek)

Renvoyer un TemporalAdjuster - "Dernier jour de la semaine du mois". Par exemple, rechercher le dernier MONDAY du mois.
public static TemporalAdjuster lastInMonth(DayOfWeek dayOfWeek)
Par exemple : Trouver le dernier dimanche du mois en cours :
TemporalAdjusters_lastInMonth_ex1.java
// Find the last SUNDAY in month.
TemporalAdjuster adjuster = TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY);  

LocalDate localDate = LocalDate.now();
System.out.println("Today is: " + localDate);

LocalDate lastSundayInMonth = (LocalDate) localDate.with(adjuster);  
System.out.println("lastSundayInMonth: " + lastSundayInMonth);
Output:
Today is: 2021-07-05
lastSundayInMonth: 2021-07-25

12. dayOfWeekInMonth(int, DayOfWeek)

Renvoyer un TemporalAdjuster - "le(ème) jour de la semaine ordinal du mois". Par exemple, trouver le 3e MONDAY du mois.
public static TemporalAdjuster dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek)
Par exemple: Rechercher le deuxième SUNDAY de mars 2025 (date de début de l'heure DST d'été 2025 aux États-Unis).
TemporalAdjusters_dayOfWeekInMonth_ex1.java
// Find the second SUNDAY in month.
TemporalAdjuster adjuster = TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SUNDAY);  

LocalDate localDate = LocalDate.of(2025, 3, 1); // 2025-03-01
 
LocalDate secondSundayInMonth = (LocalDate) localDate.with(adjuster);   
System.out.println("secondSundayInMonth: " + secondSundayInMonth);  // 2025-03-09

13. next(DayOfWeek)

Renvoyer un TemporalAdjuster - "le prochain jour de la semaine". Par exemple, trouver le MONDAY suivant.
public static TemporalAdjuster next(DayOfWeek dayOfWeek)
Exemple : Trouver le MONDAY suivant.
TemporalAdjusters_next_ex1.java
// Find the next MONDAY
TemporalAdjuster adjuster = TemporalAdjusters.next(DayOfWeek.MONDAY);  

LocalDate localDate = LocalDate.of(2021, 7, 5); // 2021-07-05
System.out.println("localDate: " + localDate);  // 2021-07-05
System.out.println("localDate.getDayOfWeek(): " + localDate.getDayOfWeek());  // MONDAY
 
// Next MONDAY.
LocalDate nextMonday = localDate.with(adjuster);
 
System.out.println("nextMonday: " + nextMonday);  // 2021-07-12
System.out.println("nextMonday.getDayOfWeek(): " + nextMonday.getDayOfWeek());  // MONDAY
Output:
localDate: 2021-07-05
localDate.getDayOfWeek(): MONDAY
nextMonday: 2021-07-12
nextMonday.getDayOfWeek(): MONDAY

14. nextOrSame(DayOfWeek)

Renvoyer un TemporalAdjuster - "le même jour de la semaine ou le jour suivant".
public static TemporalAdjuster nextOrSame(DayOfWeek dayOfWeek)
Exemple : Renvoyer cette date si c'est le lundi, sinon renvoyer le lundi suivant.
TemporalAdjusters_nextOrSame_ex1.java
// Find the next or same MONDAY
TemporalAdjuster adjuster = TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY);  

LocalDate localDate = LocalDate.of(2021, 7, 5); // 2021-07-05
System.out.println("localDate: " + localDate);  // 2021-07-05
System.out.println("localDate.getDayOfWeek(): " + localDate.getDayOfWeek());  // MONDAY
 
// Next or same MONDAY.
LocalDate nextOrSameMonday = localDate.with(adjuster);
 
System.out.println("nextOrSameMonday: " + nextOrSameMonday);  // 2021-07-05
System.out.println("nextOrSameMonday.getDayOfWeek(): " + nextOrSameMonday.getDayOfWeek());  // MONDAY

15. previous(DayOfWeek)

Renvoyer un TemporalAdjuster - "le jour de la semaine précédent". Par exemple, rechercher le MONDAY précédent.
public static TemporalAdjuster previous(DayOfWeek dayOfWeek)
Par exemple : Rechercher MONDAY précédent.
TemporalAdjusters_previous_ex1.java
// Find the previous MONDAY
TemporalAdjuster adjuster = TemporalAdjusters.previous(DayOfWeek.MONDAY);  

LocalDate localDate = LocalDate.of(2021, 7, 5); // 2021-07-05
System.out.println("localDate: " + localDate);  // 2021-07-05
System.out.println("localDate.getDayOfWeek(): " + localDate.getDayOfWeek());  // MONDAY
 
// Previous MONDAY.
LocalDate previousMonday = localDate.with(adjuster);
 
System.out.println("previousMonday: " + previousMonday);  // 2021-06-28
System.out.println("previousMonday.getDayOfWeek(): " + previousMonday.getDayOfWeek());  // MONDAY
Output:
localDate: 2021-07-05
localDate.getDayOfWeek(): MONDAY
previousMonday: 2021-06-28
previousMonday.getDayOfWeek(): MONDAY

16. previousOrSame(DayOfWeek)

Renvoyer un TemporalAdjuster - "le même ou précédent jour de la semaine"
public static TemporalAdjuster previousOrSame(DayOfWeek dayOfWeek)
Par exemple: Renvoyer cette date si c'est lundi, sinon, renvoyer au précédent lundi.
TemporalAdjusters_previousOrSame_ex1.java
// Find the previous or same MONDAY
TemporalAdjuster adjuster = TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY);  

LocalDate localDate = LocalDate.of(2021, 7, 5); // 2021-07-05
System.out.println("localDate: " + localDate);  // 2021-07-05
System.out.println("localDate.getDayOfWeek(): " + localDate.getDayOfWeek());  // MONDAY
 
// Previous or same MONDAY.
LocalDate previousOrSameMonday = localDate.with(adjuster);
 
System.out.println("previousOrSameMonday: " + previousOrSameMonday);  // 2021-07-05
System.out.println("previousOrSameMonday.getDayOfWeek(): " + previousOrSameMonday.getDayOfWeek());  // MONDAY