java - How to find difference in dates -


i have directory structure having folders date folder names.

directory

i want delete folder except last 2 days date.in case except today's folder , last 2 days.i.e., 23,22,21. here can't use joda-time find difference between dates.

here code trying towards this.

dateformat dateformat=new simpledateformat("yyyy-mm-dd");     calendar cal=calendar.getinstance();     cal.add(calendar.date, -2);     //java.util.date date=new java.util.date();     system.out.println("the date "+dateformat.format(cal.gettime()));     string direct="d:\\tempm\\sample\\"+dateformat.format(cal.gettime());     file file=new file(direct); /*  if(!file.exists())     {         file.mkdir();         system.out.println("folder created");     }*/   string path="d:\\tempm\\sample\\";     file file2=new file(path);     for(file fi:file2.listfiles())     {         if(!fi.getabsolutepath().equals(direct))         {             system.out.println(fi.getabsolutepath());             filedeletestrategy.force.delete(fi);             system.out.println("files except todays date deleted");         }     }  

how find difference of dates format?also how subtract path case ?any ideas more helpful

thanks

here

date d1 = null; date d2 = null;     try {         d1 = date.getinstance();         d2 = format.parse(file2.getname());     } catch (parseexception e) {         e.printstacktrace();     }          long diff = d2.gettime() - d1.gettime();     long diffdays = diff / (60 * 60 * 1000 * 24);     if(diffdays<=-3)     {         // code of delete     } 

now need using name of folders date, , use date's instance other side of difference


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -