ADVERTISEMENT
ADVERTISEMENT

Java program to subtract days from the current date

Java example to demonstrate how to subtract days from the current date?
Submitted by Nidhi, on April 02, 2022

Problem Solution:

In this program, we will create an instance of the Calendar class and subtract days from the current date using add() method, and printed the result.

Program/Source Code:

The source code to subtract days from the current date is given below. The given program is compiled and executed successfully.

// Java program to subtract days from 
// the current date

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();

    cal.add(Calendar.DATE, -12);
    System.out.println("Date before 12 days: " + cal.getTime());
  }
}

Output:

Date before 12 days: Mon Mar 21 05:37:24 GMT 2022

Explanation:

In the above program, we created a class Main that contains a static method main(). The main() method is the entry point for the program, here we created the object of the Calendar class and subtracted the 12 days from the current date using add() method, and printed the result.

Java Date and Time Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.