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中減去給定單位的給定數(shù)量,并返回LocalDate。
These methods may throw an exception at the time of performing subtraction.
這些方法在執(zhí)行減法時(shí)可能會(huì)引發(fā)異常。
- ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.ArithmeticException :當(dāng)計(jì)算結(jié)果超出表示此對(duì)象的限制時(shí),可能引發(fā)此異常。
- DateTimeException: This exception may throw when getting any error during subtraction.DateTimeException :在減法過程中遇到任何錯(cuò)誤時(shí),都可能引發(fā)此異常。
- UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.UnsupportedTemporalTypeException :當(dāng)不支持給定單元時(shí),可能引發(fā)此異常。
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.
這些是非靜態(tài)方法,可通過類對(duì)象訪問,如果嘗試使用類名訪問這些方法,則會(huì)收到錯(cuò)誤消息。
Parameter(s):
參數(shù):
In the first cases, minus(TemporalAmount t_amt),
在第一種情況下,減號(hào)(TemporalAmount t_amt),
- TemporalAmount t_amt – represents the amount to be subtracted from this LocalDate.
- TemporalAmount t_amt –表示要從此LocalDate中減去的數(shù)量。
In the second cases, minus(long amt, TemporalUnit t_unit),
在第二種情況下,減號(hào)(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 –代表測(cè)量給定數(shù)量的單位。
Return value:
返回值:
In both the cases, the return type of the method is LocalDate,
在這兩種情況下,方法的返回類型均為L(zhǎng)ocalDate 。
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 LocalDateimport java.time.*; import java.time.temporal.*;public class MinusOfLocalDate {public static void main(String args[]) {long amt = 4;// Instantiates two LocalDateLocalDate 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 amtSystem.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_da1LocalDate l_date = l_da1.minus(weeks);// Display l_dateSystem.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_da2l_date = l_da2.minus(amt, ChronoUnit.MONTHS);// Display l_dateSystem.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
總結(jié)
以上是生活随笔為你收集整理的Java LocalDate类| minus()方法与示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: julia在mac环境变量_在Julia
- 下一篇: java isempty_Java Ar