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: 4l_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