Open In App

LocalDate plusWeeks() method in Java with Examples

Last Updated : 19 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The plusWeeks() method of LocalDate class in Java is used to add the number of specified week in this LocalDate and return a copy of LocalDate.For example, 2018-12-24 plus one week would result in 2018-12-31. This instance is immutable and unaffected by this method call.

Syntax:

public LocalDate plusWeeks(long weeksToAdd)

Parameters: This method accepts a single parameter weeksToAdd which represents the weeks to add, may be negative.

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

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

Below programs illustrate the plusWeeks() method:

Program 1:




// Java program to demonstrate
// LocalDate.plusWeeks() 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"
                           + " adding weeks: " + date);
  
        // add 5 weeks
        LocalDate returnvalue
            = date.plusWeeks(5);
  
        // print result
        System.out.println("LocalDate after "
                           + " adding weeks: " + returnvalue);
    }
}


Output:

LocalDate before adding weeks: 2018-12-26
LocalDate after  adding weeks: 2019-01-30

Program 2:




// Java program to demonstrate
// LocalDate.plusWeeks() 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"
                           + " adding weeks: " + date);
  
        // add -3 weeks
        LocalDate returnvalue
            = date.plusWeeks(-3);
  
        // print result
        System.out.println("LocalDate after "
                           + " adding weeks: " + returnvalue);
    }
}


Output:

LocalDate before adding weeks: 2018-12-24
LocalDate after  adding weeks: 2018-12-03

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads