java.time.YearMonth.getLong(TemporalField field)方法

java.time.YearMonth.getLong(TemporalField field)方法從此年月獲取指定字段的long值。

聲明

以下是java.time.YearMonth.getLong(TemporalField field)方法的聲明。

public long getLong(TemporalField field)

參數

  • field - 要獲取的字段,而不是null

返回值

該字段的值。

異常

  • DateTimeException - 如果無法獲取該字段的值或該值超出該字段的有效值範圍。
  • UnsupportedTemporalTypeException - 如果不支持該字段或值的範圍超過long。
  • ArithmeticException - 如果發生數字溢出

示例

以下示例顯示了java.time.YearMonth.getLong(TemporalField field)方法的用法。

package com.zaixian;

import java.time.YearMonth;
import java.time.temporal.ChronoField;

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

      YearMonth date = YearMonth.of(2017,12);
      System.out.println(date.getLong(ChronoField.YEAR_OF_ERA));
   }
}

編譯並運行上面的程式,這將產生以下結果 -

2017

上一篇: java.time.YearMonth類 下一篇: java.time.ZonedDateTime類