Java即时类| plus()方法与示例

即时类plus()方法 (Instant Class plus() method)

Syntax:

句法:

    public Instant plus(TemporalAmount t_amt);
    public Instant plus(long amt, TemporalUnit t_unit);

  • plus() method is available in java.time package.

    plus()方法在java.time包中可用。

  • plus(TemporalAmount t_amt) method is used to add the given amount to this Instant and return the Instant.

    plus(TemporalAmount t_amt)方法用于将给定金额添加到此Instant并返回Instant。

  • plus(long amt, TemporalUnit t_unit) method is used to add the given amount in the given unit to this Instant and return the Instant.

    plus(long amt,TemporalUnit t_unit)方法用于将给定单位的给定金额添加到此Instant并返回Instant。

  • These methods may throw an exception at the time of performing addition.

    这些方法在执行加法时可能会引发异常。

    • ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.ArithmeticException :当计算结果超出表示此对象的限制时,可能引发此异常。
    • UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.UnsupportedTemporalTypeException :当不支持给定单元时,可能引发此异常。
    • DateTimeException: This exception may throw when getting any error during addition.DateTimeException :在添加过程中出现任何错误时,可能引发此异常。
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, "plus(TemporalAmount t_amt)",

    在第一种情况下,“ plus(TemporalAmount t_amt)”

    • TemporalAmount t_amt – represents the amount to be added to this Instant.
    • TemporalAmount t_amt –表示要添加到此Instant的数量。
  • In the second case, "plus(long amt, TemporalUnit t_unit)",

    在第二种情况下,“ plus(long amt,TemporalUnit t_unit)”,

    • long amt – represents the amount in units to be added to this Instant.
    • long amt –表示要添加到此Instant的单位数量。
    • TemporalUnit t_unit – represents the unit to measure the given amount.
    • TemporalUnit t_unit –代表测量给定数量的单位。

Return value:

返回值:

In both the cases, the return type of the method is Instant,

在这两种情况下,方法的返回类型均为Instant 。

  • In the first case, it returns the Instant that holds the value added the given amount to this Instant.

    在第一种情况下,它返回包含将给定金额添加到此Instant的值的Instant。

  • In the second case, it returns the Instant that holds the value added the given amount in unit to this Instant.

    在第二种情况下,它会返回即时值,该值保存将给定值添加到此即时值中的值。

Example:

例:

// Java program to demonstrate the example 
// of plus() method of Instant

import java.time.*;
import java.time.temporal.*;

public class PlusOfInstant {
    public static void main(String args[]) {
        long amt = 2;

        // Instantiates two Instant and
        // a Duration object
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
        Instant ins2 = Instant.now();
        Duration du = Duration.ofSeconds(2);

        // Display ins1,ins2
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);
        System.out.println("amt to add: " + amt);
        System.out.println("du to add: " + du);

        System.out.println();

        // Here, this method adds the given amount
        // in the given unit with this Instant and
        // returns the Instant i.e. here we are
        // adding 2 hours with Instant ins1
        Instant plus_val = ins1.plus(amt, ChronoUnit.HOURS);

        // Display plus_val
        System.out.println("ins1.plus(amt,ChronoUnit.HOURS): " + plus_val);

        // Here, this method adds the given amount
        // with this Instant and returns the Instant 
        // i.e. here we are adding 2 seconds
        // with Instant ins2
        plus_val = ins2.plus(du);

        // Display plus_val
        System.out.println("ins2.plus(du): " + plus_val);
    }
}

Output

输出量

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-28T07:28:15.161687Z
amt to add: 2
du to add: PT2S

ins1.plus(amt,ChronoUnit.HOURS): 2006-04-03T07:10:15Z
ins2.plus(du): 2020-05-28T07:28:17.161687Z


翻译自: https://www.includehelp.com/java/instant-plus-method-with-example.aspx

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值