Syntax error Add week to current date using Calendar.add() method in Java

Add week to current date using Calendar.add() method in Java



Import the following package for Calendar class in Java

import java.util.Calendar;

Firstly, create a Calendar object and display the current date and time

Calendar calendar = Calendar.getInstance();
System.out.println("Current Date and Time = " + calendar.getTime());

Now, let us increment the weeks using the calendar.add() method and Calendar.WEEK_OF_YEAR constant.

calendar.add(Calendar.WEEK_OF_YEAR, 2);

The following is an example

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
      Calendar calendar = Calendar.getInstance();
      System.out.println("Current Date = " + calendar.getTime());
      // Adding 2 weeks
      calendar.add(Calendar.WEEK_OF_YEAR, 2);
      System.out.println("Updated Date = " + calendar.getTime());
   }
}

Output

Current Date = Thu Nov 22 18:13:38 UTC 2018
Updated Date = Thu Dec 06 18:13:38 UTC 2018
Updated on: 2020-06-27T13:34:17+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements