LocalDate minusWeeks() method in Java with Examples
The minusWeeks() method of a LocalDate class in Java is used to subtract the number of specified week from this LocalDate and return a copy of LocalDate.For example, 2018-12-24 minus one week would result in 2018-12-17. This instance is immutable and unaffected by this method call.
Syntax:
public LocalDate minusWeeks(long weeksToSubtract)
Parameters: This method accepts a single parameter weeksToSubtract which represents the weeks to subtract, may be negative.
Return Value: This method returns a LocalDate based on this date with the weeks subtracted, not null.
Exception: This method throws DateTimeException if the result exceeds the supported date range.
Below programs illustrate the minusWeeks() method:
Program 1:
// Java program to demonstrate // LocalDate.minusWeeks() method import java.time.*; public class GFG { public static void main(String[] args) { // create a LocalDate object LocalDate date = LocalDate.parse("2018-12-26"); // print instance System.out.println("LocalDate before" + " subtracting weeks: " + date); // subtract 2 weeks LocalDate returnvalue = date.minusWeeks(2); // print result System.out.println("LocalDate after " + " subtracting weeks: " + returnvalue); } } |
LocalDate before subtracting weeks: 2018-12-26 LocalDate after subtracting weeks: 2018-12-12
Program 2:
// Java program to demonstrate // LocalDate.minusWeeks() 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 weeks: " + date); // subtract -13 weeks LocalDate returnvalue = date.minusWeeks(-13); // print result System.out.println("LocalDate after " + " subtracting weeks: " + returnvalue); } } |
LocalDate before subtracting weeks: 2018-12-24 LocalDate after subtracting weeks: 2019-03-25
Reference:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#minusWeeks(long)
Recommended Posts:
- ZonedDateTime minusWeeks() method in Java with Examples
- OffsetDateTime minusWeeks() method in Java with examples
- LocalDate now() Method in Java with Examples
- LocalDate plus() method in Java with Examples
- LocalDate now() Method in Java with Examples
- LocalDate get() method in Java with Examples
- LocalDate with() Method in Java with Examples
- LocalDate lengthOfMonth() method in Java with Examples
- LocalDate lengthOfYear() method in Java with Examples
- LocalDate isEqual() method in Java with Examples
- LocalDate isBefore() method in Java with Examples
- LocalDate isLeapYear() method in Java with Examples
- LocalDate plusDays() Method in Java with Examples
- LocalDate getLong() method in Java with Examples
- LocalDate getEra() method in Java with Examples
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.
Improved By : Akanksha_Rai



