Java Program to decrement a Date using the Calendar Class

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

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

Now, let us decrement the date using the add() method and Calendar.DATE constant. Set a negative value since we are decrementing the date

calendar.add(Calendar.DATE, -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());
      // Decrementing date by 3
      calendar.add(Calendar.DATE, -3);
      System.out.println("Updated Date = " + calendar.getTime());
   }
}

Output

Current Date = Thu Nov 22 15:55:02 UTC 2018
Updated Date = Mon Nov 19 15:55:02 UTC 2018
raja
Published on 19-Dec-2018 10:29:38
Advertisements