Java中的 LocalTime plusHours() 方法及示例
LocalTime类的plusHours()方法用于将指定的 Hours 值添加到此 LocalTime 并将结果作为 LocalTime 对象返回。这个瞬间是不变的。计算在午夜左右结束。
句法:
public LocalTime plusHours(long hoursToAdd)
参数:此方法接受单个参数hoursToAdd ,它是要添加的小时的值,它可以是负值。
返回值:此方法基于此时间返回一个LocalTime ,并添加小时数。
下面的程序说明了 plusHours() 方法:
方案一:
// Java program to demonstrate
// LocalTime.plusHours() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time
= LocalTime.parse("19:34:50.63");
// print LocalTime
System.out.println("LocalTime before addition : "
+ time);
// added 3 hours using plusHours()
LocalTime value = time.plusHours(3);
// print result
System.out.println("LocalTime after addition : "
+ value);
}
}
输出:
LocalTime before addition : 19:34:50.630
LocalTime after addition : 22:34:50.630
方案二:
// Java program to demonstrate
// LocalTime.plusHours() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time
= LocalTime.parse("01:00:01");
// print LocalTime
System.out.println("LocalTime before addition : "
+ time);
// added -10 hours using plusHours()
LocalTime value = time.plusHours(-10);
// print result
System.out.println("LocalTime after addition : "
+ value);
}
}
输出:
LocalTime before addition : 01:00:01
LocalTime after addition : 15:00:01
参考资料: https: Java/time/LocalTime.html#plusHours(long)