Saturday, October 25, 2025
HomeLanguagesJavaTemporalAdjusters firstInMonth() method in Java with Examples

TemporalAdjusters firstInMonth() method in Java with Examples

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);
    }
}


Output:

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);
    }
}


Output:

First date in a month having TUESDAY for localdate 2023-10-11 is: 2023-10-03

References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/TemporalAdjusters.html#firstInMonth(java.time.DayOfWeek)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS