Java Program to add 3 months to the calendar


Firstly, you need to import the following package for Calendar class in Java

import java.util.Calendar;

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

calendar.add(Calendar.MONTH, 3);

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

Output

Current Date = Fri Nov 23 06:38:56 UTC 2018
Updated Date = Sat Feb 23 06:38:56 UTC 2019

Updated on: 27-Jun-2020

594 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements