java日期的加減
private static final String DATE_NUMBER_PATTERN = "yyyyMMdd";定義日期返回的類型
/**
- 由yyyyMMdd格式的字符串返回日期
- @param date
- @return */ public static Date numberToDate(String string) { if (string == null) return null; SimpleDateFormat simpleDateFormat = new SimpleDateFormat( DateUtilsC.DATE_NUMBER_PATTERN);
try { return simpleDateFormat.parse(string); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }
return null;
}
對傳過來的日期操作!!!
Date dealDate = DateUtilsC.numberToDate(dealDateStr);
Date endDate = DateUtilsC.numberToDate(endDateStr);
// 獲取日歷控件
Calendar cal = Calendar.getInstance();
// 加1天后的開始日期
cal.clear();
cal.setTime(dealDate);
cal.add(Calendar.DAY_OF_YEAR, 1);
Date dealDateD1 = cal.getTime();
// 開始日期與結束日期相差1天內
if(!dealDateD1.before(endDate)){
return "01";
}
// 加7天后的開始日期
cal.clear();
cal.setTime(dealDate);
cal.add(Calendar.DAY_OF_YEAR, 7);
Date dealDateD7 = cal.getTime();
// 開始日期與結束日期相差7天內
if(!dealDateD7.before(endDate)){
return "02";
}
// 加14天后的開始日期
cal.clear();
cal.setTime(dealDate);
cal.add(Calendar.DAY_OF_YEAR, 14);
Date dealDateD14 = cal.getTime();
// 開始日期與結束日期相差14天內
if(!dealDateD14.before(endDate)){
return "03";
}
// 加21天后的開始日期
cal.clear();
cal.setTime(dealDate);
cal.add(Calendar.DAY_OF_YEAR, 21);
Date dealDateD21 = cal.getTime();
// 開始日期與結束日期相差21天內
if(!dealDateD21.before(endDate)){
return "04";
}
// 加1個月后的開始日期
cal.clear();
cal.setTime(dealDate);
cal.add(Calendar.MONTH, 1);
Date dealDateM1 = cal.getTime();
// 開始日期與結束日期相差1月內
if(!dealDateM1.before(endDate)){
return "05";
}
// 加3個月后的開始日期
cal.clear();
cal.setTime(dealDate);
cal.add(Calendar.MONTH, 3);
Date dealDateM3 = cal.getTime();
// 開始日期與結束日期相差3月內
if(!dealDateM3.before(endDate)){
return "06";
}
// 加6個月后的開始日期
cal.clear();
cal.setTime(dealDate);
cal.add(Calendar.MONTH, 6);
Date dealDateM6 = cal.getTime();
// 開始日期與結束日期相差6月內
if(!dealDateM6.before(endDate)){
return "07";
}
// 加9個月后的開始日期
cal.clear();
cal.setTime(dealDate);
cal.add(Calendar.MONTH, 9);
Date dealDateM9 = cal.getTime();
// 開始日期與結束日期相差1月內
if(!dealDateM9.before(endDate)){
return "08";
}
// 加一年后的開始日期
cal.clear();
cal.setTime(dealDate);
cal.add(Calendar.YEAR, 1);
Date dealDateY1 = cal.getTime();
// 開始日期與結束日期相差1月內
if(!dealDateY1.before(endDate)){
return "09";
}
return null;
} </pre>