duration java_Java Duration类| 带示例的multipliedBy()方法

本文详细介绍了Java中Duration类的multipliedBy()方法,解释了如何使用此方法将Duration对象乘以一个长整型数值。同时,文章通过示例代码展示了如何应用此方法,并提到了在计算结果超出限制时可能抛出的ArithmeticException。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

duration java

持续时间类multipliedBy()方法 (Duration Class multipliedBy() method)

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

    在java.time包中提供了multipliedBy()方法

  • multipliedBy() method is used to multiply this Duration by the given value.

    使用multipliedBy()方法将此Duration乘以给定值。

  • multipliedBy() 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.

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

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

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

    ArithmeticException: This exception may throw when the calculated result value exceeds the limit.

    ArithmeticException :当计算结果值超过限制时,可能引发此异常。

Syntax:

句法:

    public Duration multipliedBy(long val);

Parameter(s):

参数:

  • long val – represents the value is to be multiplied with this Duration.

    long val –表示该值将与此持续时间相乘。

Return value:

返回值:

The return type of this method is Duration, it returns the Duration that holds the value of this Duration multiplied by the given value.

该方法的返回类型为Duration ,它返回的Duration包含此Duration的值乘以给定值。

Example:

例:

// Java program to demonstrate the example 
// of multipliedBy(long val) method of Duration

import java.time.*;

public class MultipliedByOfDuration {
    public static void main(String args[]) {
        long mul_val = 5;

        // Instantiates two Duration objects
        Duration du1 = Duration.ofHours(10);
        Duration du2 = Duration.ofMinutes(25);

        // Display du1 and du2
        System.out.println("du1: " + du1);
        System.out.println("du2: " + du2);

        System.out.println("Multiplicand: " + mul_val);

        // multiplies this Duration object(du1)
        // by the given value (mul_val) 

        Duration multiplied_val = du1.multipliedBy(mul_val);
        System.out.println("du1.multipliedBy(mul_val): " + multiplied_val);

        // multiplies this Duration object(du2)
        // by the given value (mul_val) 
        multiplied_val = du2.multipliedBy(mul_val);
        System.out.println("du2.multipliedBy(mul_val): " + multiplied_val);
    }
}

Output

输出量

du1: PT10H
du2: PT25M
Multiplicand: 5
du1.multipliedBy(mul_val): PT50H
du2.multipliedBy(mul_val): PT2H5M


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

duration java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值