ADVERTISEMENT
ADVERTISEMENT

Java LocalDate Class | minus() Method with Example

LocalDate Class isSupported() method: Here, we are going to learn about the isSupported() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on June 03, 2020

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.
  • minus(TemporalAmount t_amt) method is used to subtract the given amount from this LocalDate and return the 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.
  • 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.
    • DateTimeException: This exception may throw when getting any error during subtraction.
    • UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.
  • 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 – represents the amount to be subtracted from this LocalDate.
  • In the second cases, minus(long amt, TemporalUnit t_unit),
    • long amt – represents the amount in units to be subtracted from this LocalDate.
    • TemporalUnit t_unit – represents the unit to measure the given amount.

Return value:

In both the cases, the return type of the method is LocalDate,

  • In the first case, it returns the LocalDate that holds the value subtracted the given amount from this LocalDate.
  • In the second case, it returns the LocalDate that holds the value subtracted the given amount in unit from this 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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.