java.time.YearMonth.of() 方法示例



描述

java.time.YearMonth.of(int 年份, 月份) 根據年份和月份獲取 YearMonth 例項。

宣告

以下是 java.time.YearMonth.of(int 年份, 月份) 方法的宣告:

public static YearMonth of(int year,Month month)

引數

  • 年份 − 要表示的年份,介於 MIN_YEAR 至 MAX_YEAR 之間。

  • 月份 − 要表示的年份中的月份。

返回值

YearMonth,非空。

異常

如果年份值無效,則會丟擲 DateTimeException 異常。

示例

以下示例展示了 java.time.YearMonth.of(int 年份, int 月份) 方式的使用:

package com.tutorialspoint;

import java.time.Month;
import java.time.YearMonth;

public class YearMonthDemo {
   public static void main(String[] args) {

      YearMonth year = YearMonth.of(2017,Month.DECEMBER);
      System.out.println(year);     
   }
}

讓我們編譯並執行上述程式,將生成以下結果:

2017-12
廣告
© . All rights reserved.