Subtracting Days from a Date in Java Using Calendar
In Java, there is no direct function specifically designed to subtract a specified number of days from a date. However, the Calendar class provides a versatile solution to handle this task.
Solution:
To subtract X days from a date using the Java Calendar, follow these steps:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -X);
In this code, -X represents the number of days to be subtracted.
Example:
Assuming you have a date stored in the calendar object, to subtract 5 days from this date, you would write:
calendar.add(Calendar.DAY_OF_MONTH, -5);
This operation modifies the calendar to represent the date that is 5 days prior to the original date.
The above is the detailed content of How to Subtract Days from a Date in Java Using Calendar?. For more information, please follow other related articles on the PHP Chinese website!