📌  相关文章
📜  Java中的 OffsetTime plusHours() 方法及示例

📅  最后修改于: 2022-05-13 01:54:22.612000             🧑  作者: Mango

Java中的 OffsetTime plusHours() 方法及示例

OffsetTime 类plusHours()方法用于向此 OffsetTime 添加指定的 Hours 值,并将结果作为 OffsetTime 对象返回。这个瞬间是不变的。计算在午夜左右结束。

句法:

public OffsetTime plusHours(long hoursToAdd)

参数:此方法接受单个参数hoursToAdd ,它是要添加的小时的值,它可以是负值。

返回值:此方法基于此时间返回一个OffsetTime加上小时数。

下面的程序说明了 plusHours() 方法:

方案一:

// 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

方案二:

// 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: Java/time/OffsetTime.html#plusHours-long-