Java中的即时 getLong() 方法和示例
Instant 类的getLong(TemporalField field)方法用于从该时刻获取作为参数传递的指定字段的长值。此方法在此瞬间查询字段的值,返回的值将始终在该字段的有效值范围内。当该字段不受支持且方法无法返回 int 值时,将引发异常。
句法:
public int getLong(TemporalField field)
参数:此方法接受一个参数字段,即要获取的字段。它不应该为空。
返回:此方法返回字段的长值。
异常:此方法抛出以下异常:
- DateTimeException :如果无法获取该字段的值或该值超出该字段的有效值范围。
- UnsupportedTemporalTypeException :如果该字段不受支持或值的范围超过一个 int。
- ArithmeticException : 如果发生数值溢出。
下面的程序说明了 Instant.getLong() 方法:
方案一:
// 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
方案二:
// 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: Java Java)