java.util.Date: seven days ago

Ramy picture Ramy · Feb 4, 2011 · Viewed 59.4k times · Source

I have a report created in Jasper Reports which ONLY recognizes java.util.Date's (not Calendar or Gregorian, etc).

Is there a way to create a date 7 days prior to the current date?

Ideally, it would look something like this:

new Date(New Date() - 7)

UPDATE: I can't emphasize this enough: JasperReports DOES NOT RECOGNIZE Java Calendar objects.

Answer

Bert F picture Bert F · Feb 4, 2011

From exactly now:

long DAY_IN_MS = 1000 * 60 * 60 * 24;
new Date(System.currentTimeMillis() - (7 * DAY_IN_MS))

From arbitrary Date date:

new Date(date.getTime() - (7 * DAY_IN_MS))

Edit: As pointed out in the other answers, does not account for daylight savings time, if that's a factor.

Just to clarify that limitation I was talking about:

For people affected by daylight savings time