Alex Rivera | Logout

Get Previous Day

Asked 2009-05-26T21:05:26.893
16

Possible Duplicate:
How to determine the date one day prior to a given date in Java?

If I have a Java.Util.Date object, what is the best way to get an object representing the 24 hours in the past of it?

Edit
Report

1 Answer

48

Using Java 1.6 java.util.Calendar.add:

public static Date subtractDay(Date date) {

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_MONTH, -1);
    return cal.getTime();
}

Others suggest using Joda Time, which is currently JSR 310, and should later be included in Java itself.

answered 2009-05-26T21:10:11.673

Your Answer