Java Instant plusSeconds()方法及示例
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.630ZReturned 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.572ZReturned Instant: 2018-12-08T23:59:46.572Z
参考文献: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#plusSeconds(long)
