Java Program to get first Friday in a month

Java 8Object Oriented ProgrammingProgramming

At first, set a date −

LocalDate date = LocalDate.of(2019, Month.APRIL, 1);

Now, get the first Friday using the firstInMonth() method. Here, you have to set DayOfWeek as Friday since we want the first Friday in a month −

LocalDate firstFriday = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY));

Example

 Live Demo

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;
public class Demo {
   public static void main(String[] args) {
      LocalDate date = LocalDate.of(2019, Month.APRIL, 1);
      System.out.println("Current date = "+date);
      LocalDate firstFriday = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY));
      System.out.println("First Friday date = "+firstFriday);
   }
}

Output

Current date = 2019-04-01
First Friday date = 2019-04-05
raja
Published on 25-Apr-2019 09:51:03
Advertisements