forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaTemporalAdjustersEx.java
More file actions
38 lines (26 loc) · 1.44 KB
/
JavaTemporalAdjustersEx.java
File metadata and controls
38 lines (26 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.zetcode;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
// Temporal adjusters are used for modifying temporal objects (LodalDate, LodalDateTime, HijrahDate )
// They allow to easily calculate calculations such as finding first day of week, moth, year etc.
public class JavaTemporalAdjustersEx {
public static void main(String[] args) {
var localDate = LocalDate.now();
System.out.printf("today: %s%n", localDate);
var date1 = localDate.with(TemporalAdjusters.firstDayOfMonth());
System.out.printf("first day of month: %s%n", date1);
var date2 = localDate.with(TemporalAdjusters.lastDayOfMonth());
System.out.printf("last day of month: %s%n", date2);
var date3 = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.printf("next Monday: %s%n", date3);
var date4 = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.printf("first day of next month: %s%n", date4);
var date5 = localDate.with(TemporalAdjusters.lastDayOfYear());
System.out.printf("last day of year: %s%n", date5);
var date6 = localDate.with(TemporalAdjusters.firstDayOfYear());
System.out.printf("first day of year: %s%n", date6);
var date7 = localDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
System.out.printf("last Sunday of month: %s%n", date7);
}
}