Saturday, January 31, 2026
HomeLanguagesJavaYear atMonth(Month month) method in Java

Year atMonth(Month month) method in Java

The atMonth(Month) method of Year class in Java combines the current year object with a month passed as parameter to it to create a YearMonth object.

Syntax:

public YearMonth atMonth(Month month)

Parameter: This method accepts a single parameter month. It is the month-of-year to use. It takes a valid Month object and cannot be NULL.

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

Below programs illustrate the atMonth(Month month) method of Year in Java:
Program 1:




// Program to illustrate the atMonth(Month) 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 YearMonth with this
        // Year object and Month passed to it
        YearMonth yearMonth = thisYear.atMonth(Month.SEPTEMBER);
  
        System.out.println(yearMonth);
    }
}


Output:

2017-09

Program 2:




// Program to illustrate the atMonth(Month) 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 YearMonth with this
        // Year object and Month passed to it
        YearMonth yearMonth = thisYear.atMonth(Month.JANUARY);
  
        System.out.println(yearMonth);
    }
}


Output:

2018-01

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

RELATED ARTICLES

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
123 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS