
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Decrement a Month using the Calendar Class 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 decrement the month using the add() method and Calendar.MONTH constant. Set a negative value here since we are decrementing
calendar.add(Calendar.MONTH, -2);
The following is an example
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 Month by 2 calendar.add(Calendar.MONTH, -2); System.out.println("Updated Date (-2 Months) = " + calendar.getTime()); } }
Output
Current Date = Thu Nov 22 15:57:30 UTC 2018 Updated Date (-2 Months) = Sat Sep 22 15:57:30 UTC 2018
Advertisements