如题:日历计算不是简单的数学加减关系。
下面是典型的错误代码示例。
//首先过滤过期或失效文件
int date = EventUtil.getTime(pathname.lastModified());
int currDate = EventUtil.getTime(-1);
if (date + EventConstants.sReserveDays <= currDate) {
//过期文件
pathname.delete();
return false;
}
/**
* 根据时间Long 返回对应的年月日
*
* @param time -1表示当前时间
* @return
*/
public static int getTime(long time) {
Date date;
if (time == -1) {
date = new Date(); //当前时间,转为整数存储
} else {
date = new Date(time);
}
return Integer.parseInt(DateFormat.format("yyyyMMdd", date).toString());
}
来源:CSDN
作者:书虫676
链接:https://blog.csdn.net/logan676/article/details/103970939