Java Instant getLong()方法及示例

来源:这里教程网 时间:2026-02-17 20:51:53 作者:

Java Instant getLong()方法及示例

Instant类的 getLong(TemporalField field) 方法是用来从这个瞬间获取作为参数传递的指定字段的长值。这个方法查询这个瞬间的字段值,返回的值总是在该字段的有效范围内。当字段不被支持,方法无法返回int值时,就会产生一个异常。

语法

public int getLong(TemporalField field)

参数: 该方法接受一个参数 字段 ,它是要获取的字段。它不应该是空的。

返回: 该方法返回该字段的 长值

异常: 该方法会抛出以下异常。

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 : 930000000field : NanoOfDay is not supportedfield : MicroOfSecond || value : 930000field : MicroOfDay is not supportedfield : MilliOfSecond || value : 930field : MilliOfDay is not supportedfield : SecondOfMinute is not supportedfield : SecondOfDay is not supportedfield : MinuteOfHour is not supportedfield : MinuteOfDay is not supportedfield : HourOfAmPm is not supportedfield : ClockHourOfAmPm is not supportedfield : HourOfDay is not supportedfield : ClockHourOfDay is not supportedfield : AmPmOfDay is not supportedfield : DayOfWeek is not supportedfield : AlignedDayOfWeekInMonth is not supportedfield : AlignedDayOfWeekInYear is not supportedfield : DayOfMonth is not supportedfield : DayOfYear is not supportedfield : EpochDay is not supportedfield : AlignedWeekOfMonth is not supportedfield : AlignedWeekOfYear is not supportedfield : MonthOfYear is not supportedfield : ProlepticMonth is not supportedfield : YearOfEra is not supportedfield : Year is not supportedfield : Era is not supportedfield : InstantSeconds || value : 1546133690field : 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)

相关推荐