Java LocalDate类| minus()方法与示例

LocalDate类isSupported()方法 (LocalDate Class isSupported() method)

Syntax:

句法:

public LocalDate minus(TemporalAmount t_amt);
public LocalDate minus(long amt, TemporalUnit t_unit);

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

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

  • minus(TemporalAmount t_amt) method is used to subtract the given amount from this LocalDate and return the LocalDate.

    minus(TemporalAmount t_amt)方法用于从此LocalDate中减去给定的金额并返回LocalDate。

  • minus(long amt, TemporalUnit t_unit) method is used to subtract the given amount in the given unit from this LocalDate and return the LocalDate.

    minus(long amt,TemporalUnit t_unit)方法用于从此LocalDate中减去给定单位的给定数量,并返回LocalDate。

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

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

    • ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.ArithmeticException :当计算结果超出表示此对象的限制时,可能引发此异常。
    • DateTimeException: This exception may throw when getting any error during subtraction.DateTimeException :在减法过程中遇到任何错误时,都可能引发此异常。
    • UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.UnsupportedTemporalTypeException :当不支持给定单元时,可能引发此异常。
  • 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 cases, minus(TemporalAmount t_amt),

    在第一种情况下,减号(TemporalAmount t_amt),

    • TemporalAmount t_amt – represents the amount to be subtracted from this LocalDate.
    • TemporalAmount t_amt –表示要从此LocalDate中减去的数量。
  • In the second cases, minus(long amt, TemporalUnit t_unit),

    在第二种情况下,减号(long amt,TemporalUnit t_unit),

    • long amt – represents the amount in units to be subtracted from this LocalDate.
    • long amt-表示要从此LocalDate中减去的金额(单位)。
    • 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 LocalDate,

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

  • In the first case, it returns the LocalDate that holds the value subtracted the given amount from this LocalDate.

    在第一种情况下,它将返回LocalDate,该LocalDate保留从此LocalDate中减去给定值的值。

  • In the second case, it returns the LocalDate that holds the value subtracted the given amount in unit from this LocalDate.

    在第二种情况下,它将返回LocalDate,该LocalDate保留从此LocalDate中减去以单位为单位的给定值的值。

Example:

例:

// Java program to demonstrate the example 
// of minus() method of LocalDate

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

public class MinusOfLocalDate {
    public static void main(String args[]) {
        long amt = 4;
        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.now();

        // Instantiates a Period 
        Period weeks = Period.ofWeeks(2);

        // Display l_da1,l_da2 and amt
        System.out.println("LocalDate l_da1 and l_da2: ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);
        System.out.println("amt to subtract: " + amt);

        System.out.println();

        // Here, this method subtracts the given
        // amount from this LocalDate l_da1 i.e.
        // here we are subtracting 2 weeks from
        // this date l_da1
        LocalDate l_date = l_da1.minus(weeks);

        // Display l_date
        System.out.println("l_da1.minus(weeks): " + l_date);

        // Here, this method subtracts the given
        // amount in the given unit from 
        // this LocalDate l_da2 i.e. here
        // we are subtracting the given amt
        // (4) in the given unit (in months) 
        // from this date l_da2
        l_date = l_da2.minus(amt, ChronoUnit.MONTHS);

        // Display l_date
        System.out.println("l_da2.minus(amt,ChronoUnit.MONTHS): " + l_date);
    }
}

Output

输出量

LocalDate l_da1 and l_da2: 
l_da1: 2007-04-04
l_da2: 2020-06-03
amt to subtract: 4

l_da1.minus(weeks): 2007-03-21
l_da2.minus(amt,ChronoUnit.MONTHS): 2020-02-03


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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值