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

Java 8Object Oriented ProgrammingProgramming

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 months using the calendar.add() method and Calendar.MONTH constant.

calendar.add(Calendar.MONTH, 8);

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());
      // Add 8 months to current date
      calendar.add(Calendar.MONTH, 8);
      System.out.println("Updated Date = " + calendar.getTime());
   }
}

Output

Current Date = Thu Nov 22 16:30:08 UTC 2018
Updated Date = Mon Jul 22 16:30:08 UTC 2019
raja
Published on 06-Dec-2018 11:18:20
Advertisements