Java offsetTime getLong()方法及示例
Java中OffsetTime类的 getLong() 方法从这个时间的参数中获取指定字段的值,作为一个long。
语法:
public long getLong(TemporalField field)
参数: 该方法接受一个单一的参数 字段 ,指定要获取的字段,不能为空。
返回值: 它返回字段的值。
错误和异常: 该程序返回三个异常,描述如下。
下面的程序说明了getLong()方法:
程序1 :
// Java program to demonstrate the getLong() method import java.time.OffsetTime;import java.time.temporal.ChronoField; public class GFG { public static void main(String[] args) { // Parses the time OffsetTime time = OffsetTime.parse("11:10:10+11:00"); System.out.println("Gets the long time: " + time.getLong(ChronoField.CLOCK_HOUR_OF_DAY)); }}
输出
Gets the long time: 11
程序2 :
// Java program to demonstrate the getLong() method// Exceptions import java.time.OffsetTime;import java.time.temporal.ChronoField; public class GFG { public static void main(String[] args) { try { // Parses the time OffsetTime time = OffsetTime.parse("11:10:10+11:00"); System.out.println("Gets the long time: " + time.getLong(ChronoField.CLOCK_HOUR_OF_DAY)); } catch (Exception e) { System.out.println("Exception: " + e); } }}输出
Gets the long time: 11
参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#getLong-java.time.temporal.TemporalField-
