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
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- How to flatten a nested list in Python - Python
- How to install Packages in Sublime Text Editor - Sublime-Text
- How to kill tomcat server process using Mac Terminal Command - Tomcat
- How to install pip on macOS using terminal command [Python] - Python
- How to find Tomcat default administrator password for various versions - Tomcat
- SharePoint List excel import - This table exceeds the maximum number of supported rows - SharePoint
- What is Markdown in Jupyter Notebook with Examples - Python
- Image Compressor Tool (100% Free - No Login) - Tools