TemporalAdjusters lastDayOfMonth() method in Java with Examples

The lastDayOfMonth() method of a TemporalAdjusters class is used to return the “last day of the month” TemporalAdjuster object, which returns a new Date object set to the last day of the month.

Syntax:

public static TemporalAdjuster lastDayOfMonth()

Parameters: This method accepts nothing.

Return value: This method returns the last day of month adjuster, not null.

Below programs illustrate the TemporalAdjusters.lastDayOfMonth() method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// TemporalAdjusters.lastDayOfMonth()
  
import java.time.LocalDate;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get TemporalAdjuster with last day
        // of the month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters.lastDayOfMonth();
  
        // using adjuster for local date-time
        LocalDate localDate
            = LocalDate.of(2020, 05, 11);
        LocalDate lastDayOfMonth
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println("last day of the "
                           + "month for localdate "
                           + localDate + ": "
                           + lastDayOfMonth);
    }
}

chevron_right


Output:

last day of the month for localdate 2020-05-11: 2020-05-31

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// TemporalAdjusters.lastDayOfMonth() method
  
import java.time.LocalDate;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get TemporalAdjuster with last day
        // of the month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters
                  .lastDayOfMonth();
  
        // using adjuster for local date-time
        LocalDate localDate
            = LocalDate.of(2010, 12, 29);
        LocalDate lastDayOfMonth
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println("last day of the "
                           + "month for localdate "
                           + localDate + ": "
                           + lastDayOfMonth);
    }
}

chevron_right


Output:

last day of the month for localdate 2010-12-29: 2010-12-31

References: https://docs.oracle.com/javase/10/docs/api/java/time/format/TemporalAdjusters.html

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.