Java中的 OffsetTime plusSecond() 方法及示例
OffsetTime 类的plusSeconds()方法用于将指定的秒数添加到此 OffsetTime 中,并将结果作为 OffsetTime 对象返回。这个瞬间是不变的。计算在午夜左右结束。
句法:
public OffsetTime plusSeconds(long SecondsToAdd)
参数:此方法接受单个参数SecondsToAdd ,即要添加的 Seconds 的值,可以是负值。
返回值:此方法基于此时间返回一个OffsetTime ,并添加 Seconds。
下面的程序说明了 plusSeconds() 方法:
方案一:
// Java program to demonstrate
// OffsetTime.plusSeconds() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a OffsetTime object
OffsetTime time
= OffsetTime.parse("21:15:10+11:10");
// print OffsetTime
System.out.println("OffsetTime before addition: "
+ time);
// add 200 Seconds using plusSeconds()
OffsetTime value = time.plusSeconds(200);
// print result
System.out.println("OffsetTime after addition: "
+ value);
}
}
输出:
OffsetTime before addition: 21:15:10+11:10
OffsetTime after addition: 21:18:30+11:10
方案二:
// Java program to demonstrate
// OffsetTime.plusSeconds() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a OffsetTime object
OffsetTime time
= OffsetTime.parse("14:25:10+01:10");
// print OffsetTime
System.out.println("OffsetTime before addition: "
+ time);
// add -3400 Seconds using plusSeconds()
OffsetTime value = time.plusSeconds(-3400);
// print result
System.out.println("OffsetTime after addition: "
+ value);
}
}
输出:
OffsetTime before addition: 14:25:10+01:10
OffsetTime after addition: 13:28:30+01:10
参考资料: https: Java/time/OffsetTime.html#plusSeconds-long-