Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this short tutorial, we’ll discuss how to find the first day of the week using a LocalDate input in Java.

2. Problem Statement

We often require the first day of the week to establish the boundaries of a week for business logic, such as building a time-tracking system for employees.

Before Java 8, the JodaTime library was used to find the first day of the week. However, post-Java 8, support for the same is not available. Therefore, we’ll see how to find the first day of the week using functionalities provided as part of java.time.LocalDate class.

3. Calendar Class

We can take a day in the week and traverse back in time using the java.util.Calendar class. First, we can loop to the start of the week according to our definition of the first day (Sunday/Monday).

Let’s set up the objects of the Calendar class for the same:

Calendar calendar = Calendar.getInstance();
ZoneId zoneId = ZoneId.systemDefault();
Date date = Date.from(localDate.atStartOfDay(zoneId).toInstant());
calendar.setTime(date);

Once the calendar object is set up, we must establish a fixed day as the first day of the week. It can be Monday, according to ISO standards, or Sunday, as followed in many countries worldwide (for instance, the USA). We can keep traversing in a loop until we reach our established first day of the week:

while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
    calendar.add(Calendar.DATE, -1);
}

We can see that subtracting one day at a time until we reach Monday helps us retrieve the date of the first day of the week. Calendar.MONDAY is a constant defined in the Calendar class. We can now convert the calendar date to a java.time.LocalDate:

LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()).toLocalDate()

4. TemporalAdjuster

TemporalAdjuster allows us to perform complex date manipulations. For example, we can obtain the next Sunday’s date, the current month’s last day, or the next year’s first day.

We can use this to determine the date of Monday or Sunday in a week, according to how we establish the first day of the week:

DayOfWeek weekStart = DayOfWeek.MONDAY;
return localDate.with(TemporalAdjusters.previousOrSame(weekStart));

The previousOrSame function returns a TemporalAdjuster. The TemporalAdjuster returns the previous occurrence of the specified week or the same day of the week if the current date is already on that day. We can use it to adjust a date and calculate the start of the week for a given date.

5. TemporalField

TemporalField represents a field of date-time, such as month-of-year or hour-of-minute. We can adjust the input date to get the first day of the week for the given input date.

We can use the dayOfWeek function to access the first day of the week based on WeekFields. The Java Date and Time API’s WeekFields class represents the week-based year and its components, including the week number, day of the week, and week-based year.

When the first day of the week is Sunday, the numbering of days of the week is from 1 to 7, with 1 being Sunday and 7 being Saturday. It provides a convenient way to work with ISO week dates. This can help us get the date of the first day of the week:

TemporalField fieldISO = WeekFields.of(locale).dayOfWeek();
return localDate.with(fieldISO, 1);

We are passing on the locale in this scenario; thus, the definition of the first day of the week being Sunday or Monday would be region dependent. To avoid this, we can use the ISO standard, which accepts Monday as the first day of the week:

TemporalField dayOfWeek = WeekFields.ISO.dayOfWeek();
return localDate.with(dayOfWeek, dayOfWeek.range().getMinimum());

The code snippet returns the date of the first day of the week for a given LocalDate instance using the ISO calendar system, which starts with Monday as the first day of the week. It achieves this by setting the day of the week field to the minimum valid value (i.e., 1 for Monday) for the given LocalDate instance.

6. Conclusion

In this article, we retrieved the date of the first day of the week from LocalDate in Java. We saw how to do the same using Calendar class and multiple ways using TemporalAdjuster and TemporalField

As always, the code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.