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

calendar.add(Calendar.DATE, -40);

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 40 Days
      calendar.add(Calendar.DATE, -40);
      System.out.println("Updated Date = " + calendar.getTime());
   }
}

Output

Current Date = Thu Nov 29 18:21:27 UTC 2018
Updated Date = Fri Oct20 18:21:27 UTC 2018

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 27-Jun-2020

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements