Java offsetTime plusHours()方法及示例
OffsetTime类的 plusHours() 方法用于向这个OffsetTime添加指定的小时数,并将结果作为一个OffsetTime对象返回。这个瞬间是不可改变的。计算过程围绕着午夜进行。
语法
public OffsetTime plusHours(long hoursToAdd)
参数: 该方法接受一个参数hoursToAdd,它是要添加的小时值,它可以是一个负值。
返回值: 该方法返回一个基于该时间并加上小时的OffsetTime。
下面的程序说明了plusHours()方法。
程序1 :
// Java program to demonstrate
// OffsetTime.plusHours() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a OffsetTime object
OffsetTime time
= OffsetTime.parse("12:16:10+11:00");
// print OffsetTime
System.out.println("OffsetTime before addition : "
+ time);
// added 3 hours using plusHours()
OffsetTime value = time.plusHours(3);
// print result
System.out.println("OffsetTime after addition : "
+ value);
}
}
输出。
OffsetTime before addition : 12:16:10+11:00
OffsetTime after addition : 15:16:10+11:00
程序2
// Java program to demonstrate
// OffsetTime.plusHours() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a OffsetTime object
OffsetTime time
= OffsetTime.parse("13:20:15+13:00");
// print OffsetTime
System.out.println("OffsetTime before addition : "
+ time);
// added -10 hours using plusHours()
OffsetTime value = time.plusHours(-10);
// print result
System.out.println("OffsetTime after addition : "
+ value);
}
}
输出。
OffsetTime before addition : 13:20:15+13:00
OffsetTime after addition : 03:20:15+13:00
参考文献 : https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#plusHours-long-