LocalDate minusDays() method in Java with Examples

The minusDays() method of LocalDate class in Java is used to subtract the number of specified day from this LocalDate and return a copy of LocalDate.For example, 2018-01-01 minus one day would result in 2018-12-31.This instance is immutable and unaffected by this method call.

Syntax:

public LocalDate minusDays(long daysToSubtract)

Parameters: This method accepts a single parameter daysToSubtract which represents the days to subtract, may be negative.

Return value: This method returns a LocalDate based on this date with the days subtracted, not null.

Exception: This method throws DateTimeException if the result exceeds the supported date range.



Below programs illustrate the minusDays() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDate.minusDays() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDate object
        LocalDate date
            = LocalDate.parse("2018-11-13");
  
        // print instance
        System.out.println("LocalDate before"
                           + " subtracting days: " + date);
  
        // subtract 17 days
        LocalDate returnvalue
            = date.minusDays(17);
  
        // print result
        System.out.println("LocalDate after "
                           + " subtracting days: " + returnvalue);
    }
}

chevron_right


Output:

LocalDate before subtracting days: 2018-11-13
LocalDate after  subtracting days: 2018-10-27

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDate.minusDays() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDate object
        LocalDate date
            = LocalDate.parse("2018-12-24");
  
        // print instance
        System.out.println("LocalDate before"
                           + " subtracting days: " + date);
  
        // subtract -15 days
        LocalDate returnvalue
            = date.minusDays(-15);
  
        // print result
        System.out.println("LocalDate after "
                           + " subtracting days: " + returnvalue);
    }
}

chevron_right


Output:

LocalDate before subtracting days: 2018-12-24
LocalDate after  subtracting days: 2019-01-08

Refrence:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#minusDays(long)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.