Java中YearMonth類的getLong()方法用於從此year-month中獲取長字段形式的指定字段的值。此方法在year-month中查詢指定字段的值。如果由於不支持該字段或出於某些其他原因而無法返回該值,則會引發異常。
用法:
public long getLong(TemporalField field)
參數:此方法接受field作為參數,該參數表示需要其值的TemporalField。
返回值:此方法返回該字段的long值。
異常:此方法引發以下異常:
- DateTimeException-如果無法獲取該字段的值,或者該值超出該字段的有效值範圍。
- UnsupportedTemporalTypeException-如果不支持該字段或值的範圍超過int。
- ArithmeticException-如果發生數字溢出。
以下示例程序旨在說明Java中YearMonth的getLong()方法:
程序1:
// Java program to demonstrate
// YearMonth.getLong() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create YearMonth object
YearMonth yearmonth
= YearMonth.of(2019, 4);
// apply getLong() method
// of YearMonth class to get year
long year
= yearmonth.getLong(
ChronoField.YEAR_OF_ERA);
// It will store only year
// in variable of type long
// print results
System.out.println("YEAR:" + year);
}
}
輸出:
YEAR:2019
程序2:
// Java program to demonstrate
// YearMonth.getLong() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create YearMonth object
YearMonth yearmonth
= YearMonth.of(2019, 4);
// apply getLong() method
// of YearMonth class to get month
long month
= yearmonth.getLong(
ChronoField.MONTH_OF_YEAR);
// It will store only month
// in variable of type long
// print results
System.out.println("MONTH:" + month);
}
}
輸出:
MONTH:4
相關用法
- Java Instant getLong()用法及代碼示例
- Java ChronoZonedDateTime getLong()用法及代碼示例
- Java ByteBuffer getLong()用法及代碼示例
- Java LocalDate getLong()用法及代碼示例
- Java Field getLong()用法及代碼示例
- Java OffsetDateTime getLong()用法及代碼示例
- Java Year getLong()用法及代碼示例
- Java ChronoLocalDateTime getLong()用法及代碼示例
- Java ZonedDateTime getLong()用法及代碼示例
- Java OffsetTime getLong()用法及代碼示例
- Java ChronoLocalDate getLong()用法及代碼示例
- Java LocalTime getLong()用法及代碼示例
- Java MonthDay getLong()用法及代碼示例
- Java ZoneOffset getLong(TemporalField)用法及代碼示例
- Java YearMonth now()用法及代碼示例
注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 YearMonth getLong() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。