7

Below is code I am using to access the date in past, 10 days ago. The output is '20130103' which is today's date. How can I return todays date - 10 days ? I'm restricted to using the built in java date classes, so cannot use joda time.

package past.date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PastDate {

    public static void main(String args[]){

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date myDate = new Date(System.currentTimeMillis());
        Date oneDayBefore = new Date(myDate.getTime() - 10);    
        String dateStr = dateFormat.format(oneDayBefore);      
        System.out.println("result is "+dateStr);

    }

}
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 1
    Have you looked at the [javadocs](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html)? The date constructor takes a value in milliseconds, not a value in days. Further, to take timezone/DST issues into account, rather look at `Calendar`. – BalusC Jan 03 '13 at 14:24
  • modify this answer: http://stackoverflow.com/questions/5894726/how-do-i-do-calendar-arithmetic-with-java-util-date – mcalex Jan 03 '13 at 14:25
  • Doesn't answer your question per se, but you might be interested in http://stackoverflow.com/questions/11727933/add-30-days-to-date-in-java?rq=1 This tells you how to add to the days, maybe you could transpose it to subtract. – ddavison Jan 03 '13 at 14:27
  • @mcalex he said clearly he's limited to the `Date` class, otherwise, good idea. – ddavison Jan 03 '13 at 14:27
  • 3
    @sir: he just said that he's restricted to "built in classes". `Calendar` is one of them. – BalusC Jan 03 '13 at 14:33
  • @sircapsalot I guess your clearly is clearer than mine. I would include **java.util**.Calendar as one of the built in java date classes. It certainly isn't joda. – mcalex Jan 03 '13 at 14:42
  • @mcalex I agree with you, most of the methods in the `Date` class are deprecated. – JavaNewbie_M107 Jan 03 '13 at 14:51

6 Answers6

15

you could manipulate a date with Calendar's methods.

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Date myDate = new Date(System.currentTimeMillis());
System.out.println("result is "+ dateFormat.format(myDate));
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.DATE, -10);
System.out.println(dateFormat.format(cal.getTime()));
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
PermGenError
  • 45,977
  • 8
  • 87
  • 106
6

This line

Date oneDayBefore = new Date(myDate.getTime() - 10);    

sets the date back 10 milliseconds, not 10 days. The easiest solution would be to just subtract the number of milliseconds in 10 days:

Date tenDaysBefore = new Date(myDate.getTime() - (10 * 24 * 60 * 60 * 1000));    
Keppil
  • 45,603
  • 8
  • 97
  • 119
  • 1
    It's a bit dodgy working directly with the milliseconds like that - will come undone by daylight savings. Saver to use the Calendar inbuilt methods. – John Farrelly Jan 03 '13 at 14:29
3

Use Calendar.add(Calendar.DAY_OF_MONTH, -10).

martijno
  • 1,723
  • 1
  • 23
  • 53
2

The class Date represents a specific instant in time, with millisecond precision.

Date oneDayBefore = new Date(myDate.getTime() - 10); 

So here you subtract only 10 milliseconds, but you need to subtract 10 days by multiplying it by 10 * 24 * 60 * 60 * 1000

Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
2

You can also do it like this:

Date tenDaysBefore = DateUtils.addDays(new Date(), -10);
syd
  • 197
  • 12
1
Date today = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(today);
cal.add(Calendar.DAY_OF_MONTH, -30);
Date today30 = cal.getTime();
System.out.println(today30);
  • While this code may resolve the poster's issue, a few lines of explanation would help everyone who reads this post to understand the solution. – Thom Aug 18 '15 at 13:48