Java Instant getLong()方法及示例

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 : 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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程