Java Data Type How to - Determine the date one day prior to a given date








Question

We would like to know how to determine the date one day prior to a given date.

Answer

import static java.util.Calendar.DAY_OF_MONTH;
import static java.util.Calendar.MARCH;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;
//from w w  w.  j  av a2  s.  co  m
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {

  public static void main(String... args) {
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(YEAR, 2015);
    calendar.set(MONTH, MARCH);
    calendar.set(DAY_OF_MONTH, 1);
    System.out.println(calendar.getTime());
    calendar.add(DAY_OF_MONTH, -1);
    System.out.println(calendar.getTime());

  }
}

The code above generates the following result.