Java Program to subtract 1 year from 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 subtract 1 year using the calendar.add() method and Calendar.YEAR constant. Set a negative value since we are decrementing here −

calendar.add(Calendar.YEAR, -1);

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());
      // Subtract 1 year from the Calendar
      calendar.add(Calendar.YEAR, -1);
      System.out.println("Updated Date = " + calendar.getTime());
   }
}

Output

Current Date = Fri Nov 23 06:39:40 UTC 2018
Updated Date = Thu Nov 23 06:39:40 UTC 2017

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 27-Jun-2020

909 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements