Instant類的getLong(TemporalField field)方法用於從此瞬間獲取作為參數傳遞的指定字段的long值。此方法在瞬間查詢該字段的值,並且返回的值將始終在該字段的值的有效範圍內。當不支持該字段並且方法無法返回int值時,將引發異常。
用法:
public int getLong(TemporalField field)
參數:此方法接受一個參數字段,該字段是要獲取的字段。它不能為空。
返回值:此方法返回該字段的long值。
異常:此方法引發以下異常:
- DateTimeException:如果無法獲取該字段的值,或者該值超出該字段的有效值範圍。
- UnsupportedTemporalTypeException:如果不支持該字段或值的範圍超過int。
- ArithmeticException:如果發生數字溢出。
以下示例程序旨在說明Instant.getLong()方法:
示例1:
// Java program to demonstrate
// Instant.getLong(TemporalField field) method
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant
= Instant.parse("2018-12-30T01:34:50.93Z");
// get all enum of chronofield
// and iterate through all enum values
for (ChronoField field : ChronoField.values()) {
try {
// get long value of field
long value = instant.getLong(field);
System.out.println("field : " + field
+ " || value : " + value);
}
catch (Exception e) {
System.out.println("field : " + field
+ " is not supported");
}
}
}
}
輸出:
field : NanoOfSecond || value : 930000000 field : NanoOfDay is not supported field : MicroOfSecond || value : 930000 field : MicroOfDay is not supported field : MilliOfSecond || value : 930 field : MilliOfDay is not supported field : SecondOfMinute is not supported field : SecondOfDay is not supported field : MinuteOfHour is not supported field : MinuteOfDay is not supported field : HourOfAmPm is not supported field : ClockHourOfAmPm is not supported field : HourOfDay is not supported field : ClockHourOfDay is not supported field : AmPmOfDay is not supported field : DayOfWeek is not supported field : AlignedDayOfWeekInMonth is not supported field : AlignedDayOfWeekInYear is not supported field : DayOfMonth is not supported field : DayOfYear is not supported field : EpochDay is not supported field : AlignedWeekOfMonth is not supported field : AlignedWeekOfYear is not supported field : MonthOfYear is not supported field : ProlepticMonth is not supported field : YearOfEra is not supported field : Year is not supported field : Era is not supported field : InstantSeconds || value : 1546133690 field : OffsetSeconds is not supported
示例2:
// Java program to demonstrate
// Instant.getLong(TemporalField field) method
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant
= Instant.parse("2018-12-30T01:34:50.93Z");
// get Instant second value from this Instant
// using getLong method
long secondvalue
= instant.getLong(
ChronoField.INSTANT_SECONDS);
// print result
System.out.println("Instant Seconds: "
+ secondvalue);
}
}
輸出:
Instant Seconds: 1546133690
示例3:獲取UnsupportedTemporalTypeException
// Java program to demonstrate
// Instant.getLong(TemporalField field) method
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant
= Instant.parse("2018-12-30T01:34:50.93Z");
// try to find AMPM_OF_DAY
// using ChronoField.AMPM_OF_DAY
// in getLong method
try {
long secondvalue
= instant.getLong(
ChronoField.AMPM_OF_DAY);
}
catch (Exception e) {
// print exception
System.out.println("Exception: " + e);
}
}
}
輸出:
Exception: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: AmPmOfDay
參考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#get(java.time.temporal.TemporalField)
相關用法
- Java ZonedDateTime getLong()用法及代碼示例
- Java ByteBuffer getLong()用法及代碼示例
- Java ChronoZonedDateTime getLong()用法及代碼示例
- Java OffsetDateTime getLong()用法及代碼示例
- Java LocalTime getLong()用法及代碼示例
- Java OffsetTime getLong()用法及代碼示例
- Java Year getLong()用法及代碼示例
- Java Field getLong()用法及代碼示例
- Java ChronoLocalDateTime getLong()用法及代碼示例
- Java MonthDay getLong()用法及代碼示例
- Java LocalDate getLong()用法及代碼示例
- Java ChronoLocalDate getLong()用法及代碼示例
- Java ZoneOffset getLong(TemporalField)用法及代碼示例
- Java Instant now()用法及代碼示例
- Java Instant with()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Instant getLong() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。