Write a Java Program to Subtract days from the current date


This Java program subtracts 10 days from the current date and time using the Calendar class. The program first creates a Calendar object using the getInstance() method, which returns a Calendar instance based on the current time zone and locale. It then calls the add() method on the Calendar object, passing in the Calendar.DATE field and the value -10, which subtracts 10 days from the current date and time. Finally, the program prints out the resulting date and time using the getTime() method of the Calendar object, which returns a Date object representing the current date and time.

Source Code

import java.util.*;
public class Subtract_Days
{
	public static void main(String[] args)
	{
		Calendar cal = Calendar.getInstance();
 
		cal.add(Calendar.DATE, -10);
		System.out.println("Date Before 10 Days : " + cal.getTime());
	}
}

Output

Date Before 10 Days : Wed Dec 07 14:28:18 IST 2022

Example Programs