Friday, December 12, 2025
HomeLanguagesJavaYear atMonthDay() method in Java

Year atMonthDay() method in Java

The atMonthDay(MonthDay) method of Year class in Java combines the current year object with a month-day object passed as parameter to it to create a LocalDate object.

Syntax:

public LocalDate atMonthDay(MonthDay monthDay)

Parameter: This method accepts a single parameter monthDay. It is the MonthDay object which specifies a month-day to use. It takes a valid MonthDay object and cannot be NULL.

Return Value: It returns a LocalDate object formed by the current year object and a valid MonthDate object passed as parameter to the function.

Note: If the current year is not a leap year and the MonthDay object passed as parameter specifies “29th February”, it will be automatically rounded to “28th February” in the resulting LocalDate object.

Below programs illustrate the atMonthDay(MonthDay) method of Year in Java:
Program 1:




// Program to illustrate the atMonthDay(MonthDay) method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Creates a Year object
        Year thisYear = Year.of(2017);
  
        // Creates a MonthDay object
        MonthDay monthDay = MonthDay.of(9, 15);
  
        // Creates a LocalDate with this
        // Year object and MonthDay passed to it
        LocalDate date = thisYear.atMonthDay(monthDay);
  
        System.out.println(date);
    }
}


Output:

2017-09-15

Program 2: Since 2018 is not a leap year, the day 29 will be rounded off to 28 in the below program.




// Program to illustrate the atMonthDay(MonthDay) method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Creates a Year object
        Year thisYear = Year.of(2018);
  
        // Creates a MonthDay object
        MonthDay monthDay = MonthDay.of(2, 29);
  
        // Creates a LocalDate with this
        // Year object and MonthDay passed to it
        LocalDate date = thisYear.atMonthDay(monthDay);
  
        System.out.println(date);
    }
}


Output:

2018-02-28

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#atMonthDay-java.time.MonthDay-

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

1 COMMENT

Most Popular

Dominic
32444 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11951 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12028 POSTS0 COMMENTS
Shaida Kate Naidoo
6945 POSTS0 COMMENTS
Ted Musemwa
7198 POSTS0 COMMENTS
Thapelo Manthata
6892 POSTS0 COMMENTS
Umr Jansen
6881 POSTS0 COMMENTS