Java Program to display past date from GregorianCalendar

Java 8Object Oriented ProgrammingProgramming

For GregorianCalendar class, import the following package.

import java.util.GregorianCalendar;

Here is the object.

GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

Now, let us get the past date, using the add() method with a negative value.

// past date
cal.add((GregorianCalendar.DATE), -1);

Example

 Live Demo

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 date
      cal.add((GregorianCalendar.DATE), -1);
      System.out.println("Modified date (Previous Date): " + cal.getTime());
   }
}

Output

Current date: Mon Nov 19 18:01:37 UTC 2018
Modified date (Previous Month): Fri Nov 18 18:01:37 UTC 2018
raja
Published on 17-Dec-2018 12:45:51
Advertisements