计算目标日期和当天相差天数

    /**
     * 计算目标日期和当天相差天数
     * @param date 目标日期 格式yyyy-MM-dd
     * @return int 和当天相差天数
     */
private int getDays(String date) {
Calendar today = Calendar.getInstance();
Calendar target = Calendar.getInstance();


SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date todayDate = new Date();
today.setTime(df.parse(df.format(todayDate)));
today.set(Calendar.HOUR, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
target.setTime(df.parse(date));
target.set(Calendar.HOUR, 0);
target.set(Calendar.MINUTE, 0);
target.set(Calendar.SECOND, 0);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
long intervalMilli = target.getTimeInMillis() - today.getTimeInMillis();
int days = (int) (intervalMilli / (24 * 60 * 60 * 1000));


return days;
}

你可能感兴趣的:(Android开发)