
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
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Get the current timestamp in Java - Java
- Fix: Ubuntu (Linux) - bash: sudo: command not found error - Ubuntu
- Find Sum of two numbers - C-Program
- How to insert date, time or custom timestamp in Notepad++ - NotepadPlusPlus
- Google translate in spreadsheet - Google
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..] - Java
- WhatsApp Keyboard shortcuts for Mac - WhatsApp
- This Toast was not created with Toast.makeText() : Android RuntimeException - Android