欢迎评论。。。。。
/**
*
* @param date 传入的日期
* @param month 下几个月
* @return 接下来几个月每个月的今天 最后一个月 向前推一天 可根据需要确定是否向前推
*/
public static List<String> getDateBy(Date date, Integer month) {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<String>();
for (int i = 1; i <= month; i++) {
Date d;
d = date;
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.MONTH, i);
if (i == month) {
c.add(Calendar.DATE, -1);
}
Date temp_date = c.getTime();
list.add(sdf1.format(temp_date));
}
return list;
}
public static void main(String[] args) {
List<String> list = MrOrderInfoController.getDateBy(new Date(), 12);
for (String string : list) {
System.out.println(string);
}
}
传入String的方法如下:
public static List<String> getDateBy(String date, Integer month) {
SimpleDateFormat sdf =null;
if(date.contains("/")){
sdf = new SimpleDateFormat("yyyy/MM/dd");
}else if(date.contains("-")){
sdf = new SimpleDateFormat("yyyy-MM-dd");
}else{
sdf = new SimpleDateFormat("yyyyMMdd");
}
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<String>();
for (int i = 1; i <= month; i++) {
Date d;
try {
d = sdf.parse(date);
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(c.MONTH, i);
if(i==month)c.add(c.DATE, -1);
Date temp_date = c.getTime();
list.add(sdf1.format(temp_date));
} catch (ParseException e) {
e.printStackTrace();
}
}
return list;
}