1

i want to ask you, if you help me with this problem...

I have two date:

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2009-12-31"); 
        Date date2 = sdf.parse("2010-01-31");

I want to compare a date, whether the difference in them X days

        int x = 30; // I need to delete the file if it is older than 30 days

        if(isOldThan30days(date1,date2, x)){
           //delete file
        }else{
           //nothing
        }

I hope you understand me :-). How can i do this? Thank you.

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
user3784463
  • 151
  • 1
  • 1
  • 9

2 Answers2

3

You may try like this to get the days between two dates:

int days = Days.daysBetween(date1, date2).getDays();

Then you can do this:

if(days > 30)
{
  //delete files
}
else
{
  //whatever
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Use the Calendar for that issue:

    Calendar c1 = new GregorianCalendar();
    c1.setTime(date1);

    Calendar c2 = new GregorianCalendar();
    c2.setTime(new Date());

    c1.add(Calendar.DAY_OF_MONTH, 30);

    if (c2.after(c1)){
        //delete Fiels
    }
Jens
  • 67,715
  • 15
  • 98
  • 113