We may require to get the first day or the last day of a week (or month or year) for various tasks such as reporting, scheduling, and analysis. This Java tutorial discusses quick solutions to find these dates using the TemporalAdjusters class added in Java 8 Date Time changes.
LocalDate today = LocalDate.of(2024, 3, 29);
// First and last day of the Week
LocalDate firstDayOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate lastDayOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
// First and last day of the Month
LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
// First and last day of the Year
LocalDate firstDayOfYear = today.with(TemporalAdjusters.firstDayOfYear());
LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
1. TemporalAdjusters Class
Java’s ‘java.time.temporal.TemporalAdjusters‘ class provides methods to adjust dates as per the strategy design pattern. It proved methods for adjusting a specified date.
The TemporalAdjusters class contains a standard set of adjusters, available as static methods, such as:
- finding the first or last day of the month
- finding the first day of next month
- finding the first or last day of the year
- finding the first day of next year
- finding the first or last day-of-week within a month, such as “first Wednesday in June”
- finding the next or previous day-of-week, such as “next Thursday”
We should use temporal.with(thisAdjuster) syntax for date adjustment for clean code purposes. The temporal can be any date time class such as LocalDate or LocaldateTime etc.
1. First Day and Last Day of Week
We can use the following two methods from the TemporalAdjusters for calculating the first day and the last day of week, in which the specified date lies. Note that we are assuming that the first day of the week is always Monday. If your application assumes any other day as the start of the week, then use that day.
- previousOrSame(DayOfWeek): adjusts the date to the first occurrence of the specified day-of-week.
- nextOrSame(DayOfWeek): adjusts the date to the first occurrence of the specified day-of-week.
LocalDate today = LocalDate.of(2024, 4, 26);
LocalDate firstDayOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate lastDayOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
System.out.println(firstDayOfWeek);
System.out.println(lastDayOfWeek);
The program output:
2024-04-22
2024-04-28
2. First Day and Last Day of Month
We can use the following two methods to find the first and the last day of a month in which the specified date occurs.
- firstDayOfMonth(): returns an adjuster that returns a new date set to the first day of the current month.
- lastDayOfMonth(): returns an adjuster that returns a new date set to the last day of the current month.
LocalDate today = LocalDate.of(2024, 4, 26);
LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(firstDayOfMonth);
System.out.println(lastDayOfMonth);
The program output:
2024-04-01
2024-04-30
Additionally, as a shortcut approach, we can directly use the LocalDate.withDayOfMonth(1) to jump on the first day, and from there calculate the last day of the month.
LocalDate today = LocalDate.of(2024, 4, 26);
LocalDate firstDayOfMonth = today.withDayOfMonth(1);
LocalDate lastDayOfMonth = today.withDayOfMonth(today.lengthOfMonth());
System.out.println(firstDayOfMonth);
System.out.println(lastDayOfMonth);
output:
2024-04-01
2024-04-30
3. First Day and Last Day of Year
We can use the following methods for getting the first day and the last day of the current year.
- firstDayOfYear(): returns the first day-of-year adjuster.
- lastDayOfYear(): returns the last day-of-year adjuster
LocalDate today = LocalDate.of(2024, 4, 26);
LocalDate firstDayOfYear = today.with(TemporalAdjusters.firstDayOfYear());
LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
System.out.println(firstDayOfYear);
System.out.println(lastDayOfYear);
The program output:
2024-01-01
2024-12-31
4. Conclusion
This short Java tutorial gave code snippets to quickly calculate the following:
- Get the first and last day of the current week
- Get the first and last day of the current month
- Get the first and last day of the current year
Do not forget to correctly set the week’s start and end days as per your application’s requirements.
Happy Learning !!
Comments