ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Java LocalDate Class | minusDays() Method with Example

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

LocalDate Class minusDays() method

  • minusDays() method is available in java.time package.
  • minusDays() method is used to subtract the given days from this LocalDate and return the LocalDate.
  • minusDays() 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.
  • minusDays() method may throw an exception at the time of performing subtraction.
    ArithmeticException: This exception may throw when the calculated result value exceeds the limit.

Syntax:

    public LocalDate minusDays(long day_val);

Parameter(s):

  • long day_val – represents the days to be subtracted from this LocalDate.

Return value:

The return type of this method is LocalDate, it returns the LocalDate that holds the value subtracted the given days from this LocalDate.

Example:

// Java program to demonstrate the example 
// of minusDays(long day_val) method of LocalDate

import java.time.*;

public class MinusDaysOfLocalDate {
    public static void main(String args[]) {
        long days = 10;
        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.of(2008, Month.FEBRUARY, 06);

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

        System.out.println();

        // Here, this method subtracts the given
        // days from this date l_da1 i.e. here we
        // are subtracting 10 days from the date
        // l_da1
        LocalDate minus_days = l_da1.minusDays(days);

        // Display minus_days
        System.out.println("l_da1.minusDays(days): " + minus_days);

        // Here, this method subtracts the given
        // days from this date l_da2 i.e. here we
        // are subtracting 10 days from the date
        // l_da2
        minus_days = l_da2.minusDays(days);

        // Display minus_days
        System.out.println("l_da2.minusDays(days): " + minus_days);
    }
}

Output

LocalDate l_da1,l_da2 : 
l_da1: 2007-04-04
l_da2: 2008-02-06
days to subtract: 10

l_da1.minusDays(days): 2007-03-25
l_da2.minusDays(days): 2008-01-27
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

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.