Java中的 OffsetTime getLong() 方法及示例
Java中OffsetTime类的getLong()方法从这个时间获取参数中指定字段的值作为long。
句法 :
public long getLong(TemporalField field)
参数:此方法接受单个参数字段,该字段指定要获取的字段,而不是 null。
返回值:它返回字段的值。
错误和异常:程序返回三个异常,描述如下:
- UnsupportedTemporalTypeException:如果该字段不被支持或值的范围超过一个long,则抛出它。
- DateTimeException:如果无法获取该字段的值或该值超出该字段的有效值范围,则抛出该异常。
- ArithmeticException:如果发生数值溢出则抛出
下面的程序说明了 getLong() 方法:
程序 1:
Java
// 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));
}
}
Java
// 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
方案二:
Java
// 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: Java/time/OffsetTime.html#getLong-java.time.temporal.TemporalField-