The getLong(TemporalField field) method of Instant class is used to gets the value as a long value from this instant for the specified field passed as parameter. This method queries this instant for the value of the field and the returned value will always be within the valid range of values for the field. When the field is not supported and method is unable to return int value then an exception is thrown.
Syntax:
public int getLong(TemporalField field)
Parameters: This method accepts one parameter field which is the field to get. It should not be null.
Returns: This method returns the long value for the field.
Exception: This method throws following exceptions:
- DateTimeException: if a value for the field cannot be obtained or the value is outside the range of valid values for the field.
- UnsupportedTemporalTypeException: if the field is not supported or the range of values exceeds an int.
- ArithmeticException: if numeric overflow occurs.
Below programs illustrate the Instant.getLong() method:
Program 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
Program 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
Program 3: To get 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
References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#get(java.time.temporal.TemporalField)