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
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- Convert Hex to ASCII in Java with Example - Java
- AWS CLI Change Default Output Format - AWS
- W3 HTML validator warning Unable to Determine Parse Mode - Html
- Share image and text Twitter using your Android Application Programatically - Twitter
- How to Apply Themes to Notepad++ - NotepadPlusPlus
- How to Make Android TextView Text Bold - Android
- How to remove unwanted Java imports in IntelliJ (alternative of eclipse Ctrl + Shift + O) - Eclipse
- How to enable line numbers in IntelliJ - HowTos