- 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 year 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 year.
cal.add((GregorianCalendar.YEAR), -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()); // previous year cal.add((GregorianCalendar.YEAR), -1); System.out.println("Modified date: " + cal.getTime()); } }
Output
Current date: Mon Nov 19 18:05:49 UTC 2018 Modified date: Sun Nov 19 18:05:49 UTC 2017
- Related Articles
- Java Program to display previous month from GregorianCalendar
- Java Program to display past date from GregorianCalendar
- Java Program to display previous day from GregorianCalender
- Checking for a Leap Year using GregorianCalendar in Java
- Java Program to get the day of the week from GregorianCalendar
- C# program to display the previous day
- Java Program to Get Year from Date
- Java Program to Display Dates of Calendar Year in Different Format
- Java Program to Display Name of the Weekdays in Calendar Year
- Java Program to get the previous leaf from a JTree
- Java Program to get the previous node from a JTree
- Java program to get the previous sibling from a JTree
- Java Program to subtract year from current date
- Modify Date and Time from GregorianCalendar in Java
- Java Program to subtract 1 year from the calendar

Advertisements