The firstInMonth(DayOfWeek) method of a TemporalAdjusters class is used to return a TemporalAdjuster object which can be used to get a new Date object which is the first date of the same month with the same matching DayOfWeek as passed as a parameter from any Date object on which this TemporalAdjuster is applied. Syntax:
public static TemporalAdjuster
firstInMonth(DayOfWeek dayOfWeek)
Parameters: This method accepts dayOfWeek which can be used to get a new Date object which is the first date of the same month with the same matching DayOfWeek. Return value: This method returns the first in month adjuster, not null. Below programs illustrate the TemporalAdjusters.firstInMonth() method: Program 1:Â
Java
// Java program to demonstrate// TemporalAdjusters.firstInMonth()Â
import java.time.*;import java.time.temporal.*;Â
public class GFG {Â Â Â Â public static void main(String[] args)Â Â Â Â {Â
        // get TemporalAdjuster with        // the first in month adjuster        TemporalAdjuster temporalAdjuster            = TemporalAdjusters.firstInMonth(                DayOfWeek.SUNDAY);Â
        // using adjuster for local date time        LocalDate localDate            = LocalDate.of(2023, 10, 11);        LocalDate firstInMonth            = localDate.with(temporalAdjuster);Â
        // print        System.out.println(            "First date in month having"            + " sunday for localdate "            + localDate + " is: "            + firstInMonth);    }} |
First date in month having sunday for localdate 2023-10-11 is: 2023-10-01
Program 2:Â
Java
// Java program to demonstrate// TemporalAdjusters.firstInMonth() methodÂ
import java.time.*;import java.time.temporal.*;Â
public class GFG {Â Â Â Â public static void main(String[] args)Â Â Â Â {Â
        // get TemporalAdjuster with        // the first in month adjuster        TemporalAdjuster temporalAdjuster            = TemporalAdjusters.firstInMonth(                DayOfWeek.TUESDAY);Â
        // using adjuster for local date-time        LocalDate localDate            = LocalDate.of(2023, 10, 11);        LocalDate firstInMonth            = localDate.with(temporalAdjuster);Â
        // print        System.out.println(            "First date in a month having"            + " TUESDAY for localdate "            + localDate + " is: "            + firstInMonth);    }} |
First date in a month having TUESDAY for localdate 2023-10-11 is: 2023-10-03
