LocalTime类的plusHours()方法用于将指定的“小时数”值添加到此LocalTime并将结果作为LocalTime对象返回。这一瞬间是一成不变的。计算大约在午夜结束。
用法:
public LocalTime plusHours(long hoursToAdd)
参数:此方法接受单个参数hoursToAdd,它是要添加的小时数的值,它可以是负值。
返回值:此方法基于此时间(加上小时)返回LocalTime。
以下示例程序旨在说明plusHours()方法:
示例1:
// 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
示例2:
// 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://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#plusHours(long)
相关用法
- Java OffsetTime plusHours()用法及代码示例
- Java ZonedDateTime plusHours()用法及代码示例
- Java LocalDateTime plusHours()用法及代码示例
- Java Duration plusHours(long)用法及代码示例
- Java LocalTime until()用法及代码示例
- Java LocalTime with()用法及代码示例
- Java LocalTime from()用法及代码示例
- Java LocalTime get()用法及代码示例
- Java LocalTime plus()用法及代码示例
- Java LocalTime compareTo()用法及代码示例
- Java LocalTime withHour()用法及代码示例
- Java LocalTime minusNanos()用法及代码示例
- Java LocalTime minusSeconds()用法及代码示例
- Java LocalTime getHour()用法及代码示例
- Java LocalTime equals()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LocalTime plusHours() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。