1.最近做项目要用到这两个方法就一起贴代码到这了:
public class DelfileUtils {
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
//获取子目录
String[] children = dir.list();
//递归删除目录中的子目录下文件
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
//获取几天前日期
public static String delName() {
//周期
int day = configuration.getInt("log.delDate");
//取时间
Date date=new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
//把日期往后增加一天.整数往后推,负数往前移动
calendar.add(calendar.DATE,-day);
//这个时间就是日期往后推一天的结果
date=calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(date);
return dateString;
}
}