Java Program to display previous day from GregorianCalender

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 10-Dec-2018 11:37:34
Advertisements