
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
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
- How to check your IP using bash for Windows? - Bash
- How to know the Docker Engine Version - Docker
- [jQuery] Uncaught ReferenceError: $ is not defined at index.html:5 - jQuery
- Microsoft Sign-in Error Code: 50058 (Request Id, Correlation Id and Timestamp) - Microsoft
- Instant Run requires Tools | Android | Enable ADB integration to be enabled - Android-Studio
- Bootstrap Button Colors Classes - Bootstrap
- Java java.time.Clock class code examples [Java Date Time API] - Java
- ls command sort by file size [Linix/UNIX/macOS/bash] - Linux