I need to subtract 3 days from current date and need to store that in Date variable?
Asked
Active
Viewed 6,070 times
-1
-
1[What have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? Did you take a look at [`Calendar`](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) by any chance? – MadProgrammer Sep 05 '13 at 06:23
-
possible duplicate http://stackoverflow.com/questions/1005523/how-to-add-one-day-to-a-date?rq=1 http://stackoverflow.com/questions/10795997/how-to-subtract-n-days-from-current-date-in-java?rq=1 – gjman2 Sep 05 '13 at 06:24
-
Also consider using Joda Time for all your date/time needs - it's much cleaner than `Date`/`Calendar`. – Jon Skeet Sep 05 '13 at 06:24
-
http://stackoverflow.com/questions/212321/how-to-subtract-x-days-from-a-date-using-java-calendar – schlagi123 Sep 05 '13 at 06:25
2 Answers
2
You could do this
Calender c = Calender.getInstance();
c.add(DAY_OF_MONTH,3);
c.add(DAY_OF_MONTH,-3);
Date d = c.getTime();

Michael Schmidt
- 9,090
- 13
- 56
- 80

Pratik Tari
- 198
- 1
- 8
2
Pull the milliseconds-since-epoch value out, subtract three days of milliseconds from it, shove it into a new Date object.
public static final long ONE_DAY_MILLIS = 86400 * 1000;
Date now = new Date();
Date then = new Date(now.getTime() - (3 * ONE_DAY_MILLIS));

caskey
- 12,305
- 2
- 26
- 27