当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Instant getLong()用法及代码示例


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)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Instant getLong() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。