Java offsetTime get()方法及示例
Java中OffsetTime类的 get() 方法从这个时间段获取指定字段的值,作为一个int。
语法:
public int get(TemporalField field)
参数: 该方法接受一个参数 字段 ,指定要获得的字段,而不是空的。
返回值: 它返回字段的值。
异常情况。该函数抛出三种异常,描述如下:
以下程序说明了get()方法:
程序1:
// Java program to demonstrate the get() 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("15:30:30+07:00"); // gets the time System.out.println(time.get(ChronoField.CLOCK_HOUR_OF_DAY)); }}
输出
15
程序2 :
// Java program to demonstrate the get() method 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("25:30:30+01:00"); // gets the time System.out.println(time.get(ChronoField.CLOCK_HOUR_OF_DAY)); } catch (Exception e) { System.out.println(e); } }}输出
java.time.format.DateTimeParseException: Text '25:30:30+01:00' could not be parsed: Invalid value for HourOfDay (valid values 0 - 23): 25
参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#get-java.time.temporal.TemporalField-
