Java Get First LocalDate of Current Week
Tags: Java LocalDate DayOfWeek DateUtil
In this Java tutorial, we learn how to get the first day of current week and return it as a LocalDate object in Java programming language.
How to get first LocalDate of current week in Java
In this first step, we create a new Java class name DateUtil, and implement a new static method named getFirstLocalDateOfCurrentWeek(), in this new method we based on the current date LocalDate.now() value and use the LocalDate.with(TemporalAdjuster adjuster) method to get the first day Monday of current week as Java code below.
DateUtil.java
import java.time.DayOfWeek;
import java.time.LocalDate;
public class DateUtil {
/**
* This method to get the first day of current week as a LocalDate object.
* @return the Monday LocalDate object of current week.
*/
public static LocalDate getFirstLocalDateOfCurrentWeek() {
LocalDate today = LocalDate.now();
LocalDate monday = today.with(DayOfWeek.MONDAY);
return monday;
}
}
In the following example Java code, we learn how to use the above static method to get the first day of current week as a LocalDate value in Java program.
GetFirstLocalDateOfCurrentWeekExample.java
import java.time.LocalDate;
public class GetFirstLocalDateOfCurrentWeekExample {
public static void main(String... args) {
// Get First LocalDate of Current Week
LocalDate firstDateOfWeek = DateUtil.getFirstLocalDateOfCurrentWeek();
System.out.println("Today:" + LocalDate.now());
System.out.println("Today day of week:" + LocalDate.now().getDayOfWeek());
System.out.println("\nFirst day of week date: " + firstDateOfWeek);
System.out.println("First day of week: " + firstDateOfWeek.getDayOfWeek());
}
}
Today:2022-08-20
Today day of week:SATURDAY
First day of week date: 2022-08-15
First day of week: MONDAY
Happy Coding 😊
Related Articles
Java Get First Date of Current Week
Java Get Last Date of Current Week
Java Get Last LocalDate of Current Week
Java Get First LocalDate of Current Month
Java Get Last LocalDate of Current Month
Java Get First LocalDate of Current Year
Java Get Last LocalDate of Current Year
Java Get First Day of Month from Specified Date
Java Get Last Date of Current Year
Java Get Last Date of Specified Month
Java Get Last Date of Specified Year
Java Get First Date of Current Year