java seconds_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.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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值