Get First and Last Day of Week, Month or Year in Java
The introduction of the Date and Time API in Java 8 brought a significant improvement to how dates and times are handled in Java. One of the key components of this new API is the TemporalAdjusters
class, which provides a variety of methods for date manipulation. This article explores how to use the TemporalAdjusters
class to find the first and last days of the week, month, and year. Let us delve into understanding the various methods to adjust dates to a specific day of the week, month, or year.
1. Using TemporalAdjusters in Java 8 Date Time API
The TemporalAdjusters
class in Java 8 provides a collection of pre-defined adjusters that can be used to manipulate dates. This class is part of the new Date and Time API introduced in Java 8. The TemporalAdjusters
class provides a set of utility methods for date adjustments. These methods return instances of TemporalAdjuster
which can be used with any class implementing the Temporal
interface, such as LocalDate
. Here are some commonly used methods:
firstDayOfMonth()
: Adjusts the date to the first day of the current month.lastDayOfMonth()
: Adjusts the date to the last day of the current month.firstDayOfNextMonth()
: Adjusts the date to the first day of the next month.firstDayOfYear()
: Adjusts the date to the first day of the current year.lastDayOfYear()
: Adjusts the date to the last day of the current year.firstInMonth(DayOfWeek dayOfWeek)
: Adjusts the date to the first occurrence of the specified day of the week in the current month.lastInMonth(DayOfWeek dayOfWeek)
: Adjusts the date to the last occurrence of the specified day of the week in the current month.next(DayOfWeek dayOfWeek)
: Adjusts the date to the next occurrence of the specified day of the week after the current date.previous(DayOfWeek dayOfWeek)
: Adjusts the date to the previous occurrence of the specified day of the week before the current date.
2. First Day and Last Day of Week
To find the first and last day of the week, you can use the DayOfWeek
enum along with the TemporalAdjusters
class. Here is an example:
import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; public class FirstAndLastDayOfWeek { public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalDate firstDayOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); LocalDate lastDayOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); System.out.println("Today: " + today); System.out.println("First Day of Week: " + firstDayOfWeek); System.out.println("Last Day of Week: " + lastDayOfWeek); } }
Here’s a code breakdown:
LocalDate.now()
: Gets the current date.previousOrSame(DayOfWeek.MONDAY)
: Adjusts the date to the previous Monday or stays on the same day if it is Monday.nextOrSame(DayOfWeek.SUNDAY)
: Adjusts the date to the next Sunday or stays on the same day if it is Sunday.
The code output is:
Today: 2024-06-07 First Day of Week: 2024-06-03 Last Day of Week: 2024-06-09
3. First Day and Last Day of Month
To find the first and last day of the month, you can use the firstDayOfMonth()
and lastDayOfMonth()
methods. Here is an example:
import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; public class FirstAndLastDayOfMonth { public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth()); LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth()); System.out.println("Today: " + today); System.out.println("First Day of Month: " + firstDayOfMonth); System.out.println("Last Day of Month: " + lastDayOfMonth); } }
Here’s a code breakdown:
firstDayOfMonth()
: Adjusts the date to the first day of the month.lastDayOfMonth()
: Adjusts the date to the last day of the month.
The code output is:
Today: 2024-06-07 First Day of Month: 2024-06-01 Last Day of Month: 2024-06-30
4. First Day and Last Day of Year
To find the first and last day of the year, you can use the firstDayOfYear()
and lastDayOfYear()
methods. Here is an example:
import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; public class FirstAndLastDayOfYear { public static void main(String[] args) { LocalDate today = LocalDate.now(); LocalDate firstDayOfYear = today.with(TemporalAdjusters.firstDayOfYear()); LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear()); System.out.println("Today: " + today); System.out.println("First Day of Year: " + firstDayOfYear); System.out.println("Last Day of Year: " + lastDayOfYear); } }
Here’s a code breakdown:
firstDayOfYear()
: Adjusts the date to the first day of the year.lastDayOfYear()
: Adjusts the date to the last day of the year.
The code output is:
Today: 2024-06-07 First Day of Year: 2024-01-01 Last Day of Year: 2024-12-31
5. Conclusion
In conclusion, the TemporalAdjusters
class in the Java 8 Date and Time API provides powerful and flexible methods for date manipulation. Whether you need to find the first or last day of a week, month, or year, TemporalAdjusters
offers a straightforward and efficient way to perform these adjustments. Understanding and utilizing these methods can greatly simplify date-related operations in your Java applications.