YearMonth atEndOfMonth() method in Java

The atEndOfMonth() method of YearMonth class in Java is used to return a LocalDate of the last day of month based on this YearMonth object with which it is used.

Syntax:

public LocalDate atEndOfMonth()

Parameter: This method does not accepts any parameter.



Return Value: It returns a Local Date with the last day of the current month as specified by this YearMonth object. This method does not returns a NUll value.

Below programs illustrate the atEndOfMonth() method of YearMonth in Java:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Programt to illustrate the atEndOfMonth() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
  
        // Creates a YearMonth object
        YearMonth thisYearMonth = YearMonth.of(2017, 8);
  
        // Creates a local date with this
        // YearMonth object passed to it
        // Last day of this month is 31
        LocalDate date = thisYearMonth.atEndOfMonth();
  
        System.out.println(date);
    }
}

chevron_right


Output:

2017-08-31

Program 2: This method also take cares of Leap Years.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Programt to illustrate the atEndOfMonth() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
  
        // Creates a YearMonth object
        YearMonth thisYearMonth = YearMonth.of(2016, 2);
  
        // Creates a local date with this
        // YearMonth object passed to it
        // Last day of February is
        // 29 as 2016 is a leap year
        LocalDate date = thisYearMonth.atEndOfMonth();
  
        System.out.println(date);
    }
}

chevron_right


Output:

2016-02-29

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#atEndOfMonth–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.