Write a Java program to get the next and previous Friday


This program demonstrates how to get the next and previous Friday dates from the current date using the java.time package. The java.time.LocalDate class is used to represent a date without time. The now() method is called on the LocalDate class to get the current date. TemporalAdjusters is a class that provides various methods to adjust dates. In this program, the next() and previous() methods are used with DayOfWeek.FRIDAY as the argument to get the next and previous Friday dates from the current date, respectively. The output of the program will be the next and previous Friday dates in the format of yyyy-mm-dd.

Source Code

import java.time.*;
import java.time.temporal.TemporalAdjusters;
class Get_Friday
{
	public static void main(String[] args)
    {
		LocalDate dt = LocalDate.now();    
		System.out.println("Next Friday: "+dt.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));
		System.out.println("Previous Friday: "+dt.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)));
    }
}

Output

Next Friday: 2022-11-11
Previous Friday: 2022-11-04

Example Programs