Instant类的plusSeconds()方法将作为参数传递的秒值添加到此瞬时,并将结果作为瞬时对象返回。此返回的Instant是不可变的。
用法:
public Instant plusSeconds(long secondsToAdd)
参数:此方法接受一个参数secondsToAdd,这是要添加的秒数。
返回值:添加秒数后,此方法返回Instant。
异常:此方法引发以下异常:
DateTimeException:如果结果超过最大或最小瞬间。
ArithmeticException:如果发生数字溢出。
以下示例程序旨在说明plusSeconds()方法:
示例1:
// Java program to demonstrate
// Instant.plusSeconds() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant
= Instant.parse("2018-10-28T19:34:50.63Z");
// print Instant
System.out.println("Instant: "
+ instant);
// addition of 84000 seconds to this instant
Instant returnedValue
= instant.plusSeconds(84000);
// print result
System.out.println("Returned Instant: "
+ returnedValue);
}
}
输出:
Instant: 2018-10-28T19:34:50.630Z
Returned Instant: 2018-10-29T18:54:50.630Z
示例2:
// Java program to demonstrate
// Instant.plusSeconds() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a Instant object
Instant instant = Instant.now();
// current Instant
System.out.println("Current instant: "
+ instant);
// addition of 930000 seconds
// to this instant
Instant returnedValue
= instant.plusSeconds(930000);
// print result
System.out.println("Returned Instant: "
+ returnedValue);
}
}
输出:
Current instant: 2018-11-28T05:39:46.572Z
Returned Instant: 2018-12-08T23:59:46.572Z
参考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#plusSeconds(long)