java - How to find difference in dates -
i have directory structure having folders date folder names.
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
Post a Comment