3

Possible Duplicate:
Anyone know a simple way using java calendar to subtract X days to a date?

Hi ,

Could anybody please tell me how can we subtract 45 days from the current sysdate ??

Thanks

Community
  • 1
  • 1

3 Answers3

7

You can use the Calendar class:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR , -45);
MByD
  • 135,866
  • 28
  • 264
  • 277
2

Duplicate of How to subtract X days from a date using Java calendar?. You can use the add function with negative days, e.g., add(Calendar.DATE, -5).

See http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html for the fine details.

Community
  • 1
  • 1
tofutim
  • 22,664
  • 20
  • 87
  • 148
1

Use Joda Time. :-)

import org.joda.time.DateTime;

DateTime dt = new DateTime().minusDays(45);

// Convert to java.util.Date
Date d = dt.toDate();
missingfaktor
  • 90,905
  • 62
  • 285
  • 365