Java Date Time - Year atMonth(int month) example








Year atMonth(int month) combines this year with a month to create a YearMonth.

Syntax

atMonth has the following syntax.

public YearMonth atMonth(int month)

Example

The following example shows how to use atMonth.

import java.time.Year;
import java.time.YearMonth;
/*w w w. ja va2s .co m*/
public class Main {
  public static void main(String[] args) {
    Year y = Year.of(2014);
    YearMonth l = y.atMonth(1);
    System.out.println(l);

  }
}

The code above generates the following result.





Example 2

The following code shows how to create LocalDate from month and day and current year.

import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
/* ww w  . ja va  2 s.  com*/
public class Main {
  public static void main(String[] args) {
    Month month = Month.valueOf("January".toUpperCase());

    LocalDate date = Year.now().atMonth(month).atDay(12);

    System.out.println(date);
  }
}

The code above generates the following result.