- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to display previous month from GregorianCalendar
For GregorianCalendar class, import the following package.
import java.util.GregorianCalendar;
Create an object.
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
Now, use the following field and add() method with a negative one (-1) to display the previous month.
cal.add((GregorianCalendar.MONTH), -1)
Example
import java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime()); // past month cal.add((GregorianCalendar.MONTH), -1); System.out.println("Modified date (Previous Month): " + cal.getTime()); } }
Output
Current date: Mon Nov 19 18:07:03 UTC 2018 Modified date (Previous Month): Fri Oct 19 18:07:03 UTC 2018
- Related Articles
- Java Program to display previous year from GregorianCalendar
- Java Program to display past date from GregorianCalendar
- Java Program to display previous day from GregorianCalender
- Java Program to get the day of the week from GregorianCalendar
- C# program to display the previous day
- Getting MongoDB results from the previous month\n
- Java Program to get the previous node from a JTree
- Java program to get the previous sibling from a JTree
- Java Program to get the previous leaf from a JTree
- Modify Date and Time from GregorianCalendar in Java
- Display two-digit month in Java
- GregorianCalendar Class in Java
- Display Month in MMM format in Java
- Display Month in MMMM format in Java
- Display Month of Year using Java Calendar

Advertisements