Java Program to subtract months from current date using Calendar.add() method

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 decrement the months using the calendar.add() method and Calendar.MONTH constant. Set a negative value since we are decrementing.

calendar.add(Calendar.MONTH, -10);


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


Current Date = Thu Nov 22 16:37:42 UTC 2018
Updated Date = Thu Mar 22 16:37:42 UTC 2018

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 25-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started