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:
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2018-12-30T01:34:50.93Z" );
for (ChronoField field : ChronoField.values()) {
try {
long value = instant.getLong(field);
System.out.println( "field : " + field
+ " || value : " + value);
}
catch (Exception e) {
System.out.println( "field : " + field
+ " is not supported" );
}
}
}
}
|
Output:
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:
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2018-12-30T01:34:50.93Z" );
long secondvalue
= instant.getLong(
ChronoField.INSTANT_SECONDS);
System.out.println( "Instant Seconds: "
+ secondvalue);
}
}
|
Output:
Instant Seconds: 1546133690
Program 3: To get UnsupportedTemporalTypeException
import java.time.*;
import java.time.temporal.ChronoField;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2018-12-30T01:34:50.93Z" );
try {
long secondvalue
= instant.getLong(
ChronoField.AMPM_OF_DAY);
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
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)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!