Display Previous Day from GregorianCalendar in Java



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 day.

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 Day): " + cal.getTime());
   }
}

Output

Current date: Mon Nov 19 18:12:53 UTC 2018
Modified date (Previous Day): Sun Nov 18 18:12:53 UTC 2018
Updated on: 2020-06-26T08:36:23+05:30

381 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements