|
| 1 | +package com.zetcode; |
| 2 | + |
| 3 | +import java.time.DayOfWeek; |
| 4 | +import java.time.LocalDate; |
| 5 | +import java.time.temporal.TemporalAdjusters; |
| 6 | + |
| 7 | +// Temporal adjusters are used for modifying temporal objects (LodalDate, LodalDateTime, HijrahDate ) |
| 8 | +// They allow to easily calculate calculations such as finding first day of week, moth, year etc. |
| 9 | + |
| 10 | +public class JavaTemporalAdjustersEx { |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + |
| 14 | + var localDate = LocalDate.now(); |
| 15 | + System.out.printf("today: %s%n", localDate); |
| 16 | + |
| 17 | + var date1 = localDate.with(TemporalAdjusters.firstDayOfMonth()); |
| 18 | + System.out.printf("first day of month: %s%n", date1); |
| 19 | + |
| 20 | + var date2 = localDate.with(TemporalAdjusters.lastDayOfMonth()); |
| 21 | + System.out.printf("last day of month: %s%n", date2); |
| 22 | + |
| 23 | + var date3 = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY)); |
| 24 | + System.out.printf("Next monday: %s%n", date3); |
| 25 | + |
| 26 | + var date4 = localDate.with(TemporalAdjusters.firstDayOfNextMonth()); |
| 27 | + System.out.printf("first day of next month: %s%n", date4); |
| 28 | + |
| 29 | + var date5 = localDate.with(TemporalAdjusters.lastDayOfYear()); |
| 30 | + System.out.printf("last day of year: %s%n", date5); |
| 31 | + |
| 32 | + var date6 = localDate.with(TemporalAdjusters.firstDayOfYear()); |
| 33 | + System.out.printf("first day of year: %s%n", date6); |
| 34 | + } |
| 35 | +} |
0 commit comments