Friday, April 3, 2026
HomeLanguagesJavaTemporalAdjusters lastInMonth() method in Java with Examples

TemporalAdjusters lastInMonth() method in Java with Examples

The lastInMonth(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 last 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 lastInMonth(DayOfWeek dayOfWeek)

Parameters: This method accepts dayOfWeek which can be used to get a new Date object which is the last date of the same month with the same matching DayOfWeek. Return value: This method returns the last in month adjuster, not null. Below programs illustrate the TemporalAdjusters.lastInMonth() method: Program 1: 

Java




// Java program to demonstrate
// TemporalAdjusters.lastInMonth()
 
import java.time.*;
import java.time.temporal.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // get TemporalAdjuster with
        // the last in month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters.lastInMonth(
                DayOfWeek.SUNDAY);
 
        // using adjuster for local date-time
        LocalDate localDate
            = LocalDate.of(1998, 10, 31);
        LocalDate lastInMonth
            = localDate.with(temporalAdjuster);
 
        // print
        System.out.println(
            "last date in month having"
            + " sunday for localdate "
            + localDate + " is: "
            + lastInMonth);
    }
}


Output:

last date in month having sunday for localdate 1998-10-31 is: 1998-10-25

Program 2: 

Java




// Java program to demonstrate
// TemporalAdjusters.lastInMonth() method
 
import java.time.*;
import java.time.temporal.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // get TemporalAdjuster with
        // the last in month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters.lastInMonth(
                DayOfWeek.TUESDAY);
 
        // using adjuster for local date time
        LocalDate localDate
            = LocalDate.of(2029, 12, 11);
        LocalDate lastInMonth
            = localDate.with(temporalAdjuster);
 
        // print
        System.out.println(
            "The last date in a month "
            + "having TUESDAY for localdate "
            + localDate + " is: "
            + lastInMonth);
    }
}


Output:

The last date in a month having TUESDAY for localdate 2029-12-11 is: 2029-12-25

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

RELATED ARTICLES

Most Popular

Dominic
32512 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6885 POSTS0 COMMENTS
Nicole Veronica
12006 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12100 POSTS0 COMMENTS
Shaida Kate Naidoo
7015 POSTS0 COMMENTS
Ted Musemwa
7259 POSTS0 COMMENTS
Thapelo Manthata
6971 POSTS0 COMMENTS
Umr Jansen
6960 POSTS0 COMMENTS