当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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-



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 OffsetTime plusHours() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。