Skip to content

Commit 5c6101e

Browse files
author
Jegors Cemisovs
committed
Refactor and streamline TemporalAdjusters documentation
Simplify the introduction and examples for `TemporalAdjusters` to improve clarity and focus on practical use cases. Removed redundant examples and reorganized sections to prioritize key adjusters like ordinal days and "teenth" date calculations. Enhanced consistency and readability across code snippets and explanations.
1 parent 11890c8 commit 5c6101e

File tree

1 file changed

+19
-97
lines changed

1 file changed

+19
-97
lines changed
Lines changed: 19 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
# Introduction
22

3-
While the core `java.time` classes like `LocalDate` and `LocalDateTime` provide basic methods for adding or subtracting time units, more complex date adjustments often require more sophisticated logic (e.g., finding the "second Tuesday of the month" or the "last day of the year").
3+
When working with `java.time` objects like `LocalDate`, you often need to perform complex date adjustments, such as finding the "second Tuesday of the month". Instead of calculating this manually, you can use **`TemporalAdjusters`**.
44

5-
The `java.time.temporal` package provides the [`TemporalAdjuster`][temporaladjuster-docs] functional interface for this purpose. It represents a strategy for adjusting a temporal object (like a `LocalDate`). The [`TemporalAdjusters`][temporaladjusters-docs] (note the plural 's') utility class contains static factory methods providing many common, predefined adjuster implementations.
6-
7-
## Using TemporalAdjusters
8-
9-
Temporal objects like `LocalDate` and `LocalDateTime` have a `with()` method that accepts a `TemporalAdjuster`. This method returns a *new* temporal object with the adjustment applied.
5+
The `java.time.temporal.TemporalAdjusters` (note the plural 's') utility class provides static methods for many common date adjustments. You apply these adjustments using the `.with()` method on a temporal object (like `LocalDate`).
106

117
```exercism/note
12-
Using `with(TemporalAdjuster)` returns a _new_ instance and does not update the existing instance, as all `java.time` temporal classes are immutable.
8+
Using `with(TemporalAdjuster)` returns a _new_ instance and does not update the existing instance, as `java.time` objects are immutable.
139
```
1410

1511
```java
@@ -20,121 +16,47 @@ import java.time.temporal.TemporalAdjusters;
2016

2117
LocalDate date = LocalDate.of(2024, Month.AUGUST, 15); // 2024-08-15 (a Thursday)
2218

23-
// Use a predefined adjuster from TemporalAdjusters
24-
LocalDate firstDayOfNextMonth = date.with(TemporalAdjusters.firstDayOfNextMonth());
25-
// => 2024-09-01
26-
27-
LocalDate nextSunday = date.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
28-
// => 2024-08-18
19+
// Use a predefined adjuster: find the last day of the month
20+
LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
21+
// => 2024-08-31
2922
```
3023

31-
## Common Predefined Adjusters
32-
33-
The `TemporalAdjusters` class provides many useful static methods. Here are some common examples:
24+
Here are some key adjusters useful for scheduling calculations:
3425

3526
### Finding Ordinal Day of Week
3627

37-
The `dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek)` adjuster finds the nth occurrence of a specific day of the week within the month.
28+
Use `dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek)` to find the nth occurrence of a day of the week.
3829

3930
```java
40-
LocalDate date = LocalDate.of(2024, Month.AUGUST, 15); // 2024-08-15
41-
4231
// Find the second Tuesday of August 2024
4332
LocalDate secondTuesday = date.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.TUESDAY));
4433
// => 2024-08-13
45-
46-
// Find the fourth Friday of August 2024
47-
LocalDate fourthFriday = date.with(TemporalAdjusters.dayOfWeekInMonth(4, DayOfWeek.FRIDAY));
48-
// => 2024-08-23
4934
```
5035

5136
### Finding First/Last Day of Week in Month
5237

53-
The `firstInMonth(DayOfWeek dayOfWeek)` and `lastInMonth(DayOfWeek dayOfWeek)` adjusters find the first or last occurrence of a day of the week within the month.
38+
Use `firstInMonth(DayOfWeek dayOfWeek)` or `lastInMonth(DayOfWeek dayOfWeek)`.
5439

5540
```java
56-
LocalDate date = LocalDate.of(2024, Month.AUGUST, 15); // 2024-08-15
57-
58-
// Find the first Sunday of August 2024
59-
LocalDate firstSunday = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));
60-
// => 2024-08-04
61-
6241
// Find the last Monday of August 2024
6342
LocalDate lastMonday = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY));
6443
// => 2024-08-26
6544
```
6645

67-
### Finding First/Last Day of Month/Year
68-
69-
Simpler adjusters find the first or last day of the current month or year.
70-
71-
```java
72-
LocalDate date = LocalDate.of(2024, Month.AUGUST, 15); // 2024-08-15
73-
74-
LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
75-
// => 2024-08-01
76-
77-
LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
78-
// => 2024-08-31
79-
80-
LocalDate lastDayOfYear = date.with(TemporalAdjusters.lastDayOfYear());
81-
// => 2024-12-31
82-
```
83-
84-
### Relative Adjustments (Next/Previous)
85-
86-
Adjusters like `next()`, `previous()`, `nextOrSame()`, and `previousOrSame()` find the next or previous occurrence of a given day of the week, relative to the current date.
46+
### Finding Relative Occurrences ("Teenth" dates)
8747

88-
* `next()`/`previous()`: Excludes the current date if it matches.
89-
* `nextOrSame()`/`previousOrSame()`: Includes the current date if it matches.
48+
Use `nextOrSame(DayOfWeek dayOfWeek)`. This is useful for finding dates like the "teenth" days (13th-19th). Apply it to the 13th day of the month to find the target day of the week within that "teenth" range.
9049

9150
```java
92-
LocalDate thursday = LocalDate.of(2024, Month.AUGUST, 15); // A Thursday
51+
LocalDate thirteenth = LocalDate.of(2024, Month.AUGUST, 13); // Tuesday the 13th
9352

94-
// Find the next Sunday (exclusive)
95-
LocalDate nextSunday = thursday.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
96-
// => 2024-08-18
53+
// Find the first Saturday on or after the 13th ("Teenth" Saturday)
54+
LocalDate teenthSaturday = thirteenth.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));
55+
// => 2024-08-17
9756

98-
// Find the next Thursday (exclusive)
99-
LocalDate nextThursday = thursday.with(TemporalAdjusters.next(DayOfWeek.THURSDAY));
100-
// => 2024-08-22
101-
102-
// Find the next Thursday (inclusive) - returns the same date
103-
LocalDate nextOrSameThursday = thursday.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY));
104-
// => 2024-08-15
105-
106-
// Find the previous Monday (exclusive)
107-
LocalDate previousMonday = thursday.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
108-
// => 2024-08-12
109-
```
110-
The `nextOrSame()` adjuster is particularly useful for finding dates in ranges like the "teenth" days (13th-19th) of a month, as seen in the Meetup exercise. Applying `nextOrSame(targetDayOfWeek)` to the 13th day of the month will find the correct "teenth" date.
111-
112-
## Custom Adjusters
113-
114-
Since `TemporalAdjuster` is a [`@FunctionalInterface`][functionalinterface-docs], you can create your own custom adjusters using lambda expressions for logic not covered by the predefined methods. The interface requires implementing a single method: `Temporal adjustInto(Temporal temporal)`.
115-
116-
```java
117-
// Example: An adjuster that finds the next workday (Mon-Fri), skipping weekends.
118-
TemporalAdjuster nextWorkday = temporal -> {
119-
LocalDate result = (LocalDate) temporal; // Assuming input is LocalDate
120-
do {
121-
result = result.plusDays(1);
122-
} while (result.getDayOfWeek() == DayOfWeek.SATURDAY || result.getDayOfWeek() == DayOfWeek.SUNDAY);
123-
return result;
124-
};
125-
126-
LocalDate friday = LocalDate.of(2024, Month.AUGUST, 16);
127-
LocalDate nextWorkdayDate = friday.with(nextWorkday);
128-
// => 2024-08-19 (Monday)
129-
130-
LocalDate sunday = LocalDate.of(2024, Month.AUGUST, 18);
131-
LocalDate nextWorkdayFromSunday = sunday.with(nextWorkday);
132-
// => 2024-08-19 (Monday)
57+
// Find the first Tuesday on or after the 13th ("Teenth" Tuesday)
58+
LocalDate teenthTuesday = thirteenth.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
59+
// => 2024-08-13 (returns the same date as it matches)
13360
```
134-
While custom adjusters are powerful, the predefined adjusters in the `TemporalAdjusters` class cover a wide range of common use cases.
13561

136-
[temporaladjuster-docs]: https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalAdjuster.html
137-
[temporaladjusters-docs]: https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalAdjusters.html
138-
[localdate-docs]: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
139-
[dayofweek-docs]: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html
140-
[functionalinterface-docs]: https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html
62+
Using these adjusters from the `TemporalAdjusters` class can greatly simplify date manipulation tasks.

0 commit comments

Comments
 (0)