DayOfWeek plus() method in Java with Examples

The plus() method of java.time.DayOfWeek is an in-built function in Java which takes a long integer as parameter and returns an instance of DayOfWeek after advancing some days forward or backward as specified by the passed parameter. The calculation rolls around the end of the week from Sunday to Monday. The specified period may be positive or negative.

Method Declaration:

public DayOfWeek plus(long days)

Syntax:



DayOfWeek dayOfWeekObject = dayOfWeekObject.plus(long days)

Parameters: This method takes days as parameter where:

  • days – is the number of days to advance forward or backward.
  • dayOfWeekObject – is an instance of DayOfWeek object.
  • Return Value: The function returns an instance of DayOfWeek after advancing by some days forward or backward.

    Below programs illustrate the above method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program Demonstrate plus()
    // method of DayOfWeek
    import java.time.DayOfWeek;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Getting an instance of DayOfWeek from int value
            DayOfWeek dayOfWeek = DayOfWeek.of(2);
      
            // Printing the day of the week
            // and its Int value
            System.out.println("Day of the Week : "
                               + dayOfWeek.name()
                               + " - "
                               + dayOfWeek.getValue());
      
            // Number of days to advance
            long adv = 10;
      
            // Advancing the day
            dayOfWeek = dayOfWeek.plus(adv);
      
            // Printing the day of the week and its
            // Int value after adv days
            System.out.println("Day of the Week after "
                               + adv + " days: "
                               + dayOfWeek.name() + " - "
                               + dayOfWeek.getValue());
        }
    }

    chevron_right

    
    

    Output:

    Day of the Week : TUESDAY - 2
    Day of the Week after 10 days: FRIDAY - 5
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program Demonstrate plus()
    // method of DayOfWeek
    import java.time.DayOfWeek;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Getting an instance of DayOfWeek
            // from int value
            DayOfWeek dayOfWeek = DayOfWeek.of(7);
      
            // Printing the day of the week
            // and its Int value
            System.out.println("Day of the Week : "
                               + dayOfWeek.name()
                               + " - "
                               + dayOfWeek.getValue());
      
            // Number of days to advance
            long adv = -3;
      
            // Advancing the day
            dayOfWeek = dayOfWeek.plus(adv);
      
            // Printing the day of the week and its
            // Int value after adv days
            System.out.println("Day of the Week after "
                               + adv + " days: "
                               + dayOfWeek.name()
                               + " - "
                               + dayOfWeek.getValue());
        }
    }

    chevron_right

    
    

    Output:

    Day of the Week : SUNDAY - 7
    Day of the Week after -3 days: THURSDAY - 4
    

    Reference: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#plus-long-



    My Personal Notes arrow_drop_up

    Coder Machine Learner Social Activist Vocalist

    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.