Java Get Last LocalDate of Current Week
Tags: Java LocalDate DayOfWeek DateUtil
In this Java tutorial, we learn how to get the last day of current week and return it as a LocalDate object in Java programming language.
How to get last LocalDate of current week in Java
In this first step, we create a new Java class named DateUtil, and implement the new static method named getLastLocalDateOfCurrentWeek(), this this method we follow these steps to get the last day of current week.
- Step 1: get today date using the LocalDate.now() method.
- Step 2: get the Sunday LocalDate object based on today date using LocalDate.with(TemporalAdjuster adjuster) method.
DateUtil.java
import java.time.DayOfWeek;
import java.time.LocalDate;
public class DateUtil {
/**
* This method to get the last day of current week as a LocalDate object.
* @return the Sunday LocalDate object of current week.
*/
public static LocalDate getLastLocalDateOfCurrentWeek() {
LocalDate today = LocalDate.now();
LocalDate sunday = today.with(DayOfWeek.SUNDAY);
return sunday;
}
}
In the following example Java code, we learn how to use the getLastLocalDateOfCurrentWeek() static method above to get last day of current week as LocalDate object in Java program.
GetLastLocalDateOfCurrentWeekExample.java
import java.time.LocalDate;
public class GetLastLocalDateOfCurrentWeekExample {
public static void main(String... args) {
// Get Last LocalDate of Current Week
LocalDate lastDateOfWeek = DateUtil.getLastLocalDateOfCurrentWeek();
System.out.println("Today:" + LocalDate.now());
System.out.println("Today day of week:" + LocalDate.now().getDayOfWeek());
System.out.println("\nLast day of week date: " + lastDateOfWeek);
System.out.println("Last day of week: " + lastDateOfWeek.getDayOfWeek());
}
}
Today:2022-08-20
Today day of week:SATURDAY
Last day of week date: 2022-08-21
Last day of week: SUNDAY
Happy Coding 😊
Related Articles
Java Get First Date of Current Week
Java Get Last Date of Current Week
Java Get First 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