duration java_Java Duration类| 带有示例的减去方法

duration java

持续时间类减去方法 (Duration Class subtractFrom() method)

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

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

  • subtractFrom() method is used to return the Temporal object subtracted this Duration from the given Temporal object.

    excludeFrom()方法用于返回从给定的Temporal对象减去此Duration的Temporal对象。

  • subtractFrom() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    tractorFrom()方法是一个非静态方法,只能使用类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。

  • subtractFrom() method may throw an exception at the time of performing subtraction.

    在执行减法时, subtractFrom()方法可能会引发异常。

    • ArithmeticException: This exception may throw when the calculated results exceed the length of this Duration.ArithmeticException :当计算结果超过此Duration的长度时,可能引发此异常。
    • DateTimeException: This exception may throw when the given object couldn't subtract this Duration.DateTimeException :如果给定对象无法减去此Duration,则可能引发此异常。

Syntax:

句法:

    public Temporal subtractFrom(Temporal temp);

Parameter(s):

参数:

  • Temporal temp – represents the object by which to subtract this Duration.

    临时温度 –表示要减去此工期的对象。

Return value:

返回值:

The return type of this method is Duration, it returns the Temporal that holds the value subtracted this Duration from the given Temporal object.

此方法的返回类型为Duration ,它返回Temporal,该Temporal保留从给定Temporal对象减去此Duration的值。

Example:

例:

// Java program to demonstrate the example 
// of subtractFrom(Temporal temp) method 
// of Duration

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

public class SubtractFromDuration {
    public static void main(String args[]) {
        // Instantiates a Duration 
        // and LocalTime object
        Duration du = Duration.ofHours(2);
        LocalTime l_time = LocalTime.now();

        // Display du and l_time
        System.out.println("du to subtract: " + du);
        System.out.println("l_time: " + l_time);

        // subtracts this object (du) from the given 
        // object (l_time) i.e. we are subtracting
        // duration (2 hrs) from l_time hours unit
        l_time = (LocalTime) du.subtractFrom(l_time);

        // Display updated l_time
        System.out.println("du.subtractFrom(l_time): " + l_time);
    }
}

Output

输出量

du to subtract: PT2H
l_time: 08:20:02.395444
du.subtractFrom(l_time): 06:20:02.395444


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

duration java

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值