
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Subtract days from current date using Calendar.DATE in Java
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 subtract the days using the add() method and Calendar.DATE constant. Set a negative value here since we are decrementing.
calendar.add(Calendar.DATE, -2);
Example
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 days by 2 calendar.add(Calendar.DATE, -2); System.out.println("Updated Date = " + calendar.getTime()); } }
Output
Current Date = Thu Nov 22 16:09:20 UTC 2018 Updated Date = Tue Nov 20 16:09:20 UTC 2018
- Related Questions & Answers
- Java Program to subtract week from current date
- Java Program to subtract year from current date
- Java Program to subtract months from current date using Calendar.add() method
- How to subtract days from a date in JavaScript?
- How to subtract 10 days from the current datetime in MySQL?
- How to subtract 30 days from the current datetime in MySQL?
- Java Program to add days to current date using Java Calendar
- Subtract minutes from current time using Calendar.add() method in Java
- Subtract seconds from current time using Calendar.add() method in Java
- Java Program to subtract 40 days from the calendar
- Java Program to subtract hours from current time using Calendar.add() method
- How to subtract number of days from a date to get the previous date in R?
- Is there a way to subtract number of days from a date in MySQL?
- Add 11 days to current date in MySQL
- How to subtract 1 hour from current time using Swift?
Advertisements