根据条件筛选中的日期
private static List<Date> dealByDate(Date st, Date ed, String type,
Integer limit) {
List<Date> tempdate = new ArrayList<Date>();
Calendar calendar = new GregorianCalendar();
if (type.equals("1")) {// 一次性的
tempdate.add(ed);
return tempdate;
}
Date date = new Date();
if (date.getTime() > st.getTime()) {
} else {
}
if (type.equals("24")) {// 按天累加
while (ed.after(calendar.getTime())) {
calendar.add(Calendar.DAY_OF_MONTH, limit);
tempdate.add(calendar.getTime());
}
}
if (type.equals("7")) {// 按周累加
while (ed.after(calendar.getTime())) {
calendar.add(Calendar.DAY_OF_MONTH, limit * 7);
tempdate.add(calendar.getTime());
}
}
if (type.equals("30")) {// 按月累加
while (ed.after(calendar.getTime())) {
calendar.add(Calendar.DAY_OF_MONTH, limit * 30);
tempdate.add(calendar.getTime());
}
}
if (type.equals("365")) {// 按月累加
while (ed.after(calendar.getTime())) {
calendar.add(Calendar.YEAR, limit);
tempdate.add(calendar.getTime());
}
}
List<Date> realdate = new ArrayList<Date>();
for (Date date2 : tempdate) {
if(ed.after(date2)){
realdate.add(date2);
}
}
return realdate;
}
public static void main(String[] args) throws ParseException {
Date st = new Date();
Date ed = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
String dstr = "2016-01-28";
String detr = "2017-02-16";
try {
st = sdf.parse(dstr);
ed = sdf.parse(detr);
} catch (ParseException e) {
e.printStackTrace();
}
String type = "365";
Integer limit = 1;
List<Date> date = dealByDate(st, ed, type, limit);
for (Date date2 : date) {
System.out.println(date2.toLocaleString());
}
}