Java 8: Get First and Last Date of the Week for Given Date


First and Last Date of Week - Java 8 Example
First and Last Date of Week - Java 8 Example

In order to get the first and the last date of the week using the Java 8+ Date & Time API, make use of the WeekFields, DayOfWeek and TemporalAdjusters classes,

First let's get a LocalDate for which we want to get the first and last dates of the week,

LocalDate localDate = LocalDate.of(2022,1,1);

Now you must know which Locale you are working on, based on the region you are in the first and the last day of the week vary! We will take Local as the US in our example,

Locale UnitedStates = Locale.US;

Now lets make create java.time.temporal.WeekFields class object and get the first day of the Week using method: getFirstDayOfWeek()

WeekFields weekFields = WeekFields.of(UnitedStates);
DayOfWeek firstDayOfTheWeek = weekFields.getFirstDayOfWeek();

To know the last day of the week we do not have any method in WeekFields, the trick here is just to make use of the minus method on firstDayOfTheWeek to get the last day!

DayOfWeek lastDayOfTheWeek = firstDayOfTheWeek.minus(1);

Make use of the TemporalAdjusters previousOrSame method to get the first Date of the Week,

LocalDate firstDate = date.with(TemporalAdjusters.previousOrSame(firstDayOfTheWeek));

Similiary, use of the TemporalAdjusters nextOrSame method to get the last Date of the Week,

LocalDate lastDate = date.with(TemporalAdjusters.nextOrSame(lastDayOfTheWeek));
Complete Code Example:
package org.code2care;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Locale;

/**
 * Java Programs by Code2care.org
 */
public class Java8DateTimeExample {

    public static void main(String[] args) {

        LocalDate now = LocalDate.now();
        System.out.println("Input Date: "+ now);
        System.out.println(getFirstAndLastDayOfTheWeek(now));

        LocalDate localDate = LocalDate.of(2022,01,01);
        System.out.println("Input Date: "+ localDate);
        System.out.println(getFirstAndLastDayOfTheWeek(localDate));

    }

    /**
     * 
     * To get the first and last date of the passed in date.
     * 
     * @param date LocalDate
     * @return string with first and last date in ISO-8601 format
     */
    public static String getFirstAndLastDayOfTheWeek(LocalDate date) {

        Locale UnitedStates = Locale.US;
        WeekFields weekFields = WeekFields.of(UnitedStates);
        DayOfWeek firstDayOfTheWeek = weekFields.getFirstDayOfWeek();
        DayOfWeek lastDayOfTheWeek = firstDayOfTheWeek.minus(1);

        LocalDate firstDate = date.with(TemporalAdjusters.previousOrSame(firstDayOfTheWeek));
        LocalDate lastDate = date.with(TemporalAdjusters.nextOrSame(lastDayOfTheWeek));
        return firstDate +" - " + lastDate;
    }
}
Output:

Input Date: 2022-05-14
2022-05-08 - 2022-05-14

Input Date: 2022-01-01
2021-12-26 - 2022-01-01

Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.