Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Previous Day [duplicate]

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?

like image 576
Nathaniel Flath Avatar asked May 26 '09 21:05

Nathaniel Flath


1 Answers

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.

like image 173
Jared Oberhaus Avatar answered Oct 02 '22 10:10

Jared Oberhaus