类似于人生规划,输入生日,往后延伸100年,一个月为1个格子,100年则为1200格子,格子从数据库里出来循环显示在网页上,要判断每个格子的日期方便做增删改查。
所以你的问题是什么呢
你这有个漏洞,2.29的怎么算?
说明:28号以后,不足天数的月份会变成当月最后一天,此后不变,你这里没这部分详细规则,大致写了一下
public class Main {
public static void main(String[] args) {
Main main = new Main();
List<Date> dates = main.get1200Date(main.getDate("2012-02-29","yyyy-MM-dd"));
for(int i = 0; i < dates.size(); i++){
System.out.println(main.getDateStr(dates.get(i),"yyyy-MM-dd"));
}
}
private List<Date> get1200Date(Date start){
Calendar c=Calendar.getInstance();//开始时间
List<Date> ans = new ArrayList<>();
c.setTime(start);
for(int i = 1; i <= 1200; i++){
//叠加1的话,28号以后会最终变成28,会有误差
c.add(Calendar.MONTH, i);
ans.add(c.getTime());
c.setTime(start);
}
return ans;
}
private String getDateStr(Date date,String forMat) {
SimpleDateFormat dateFormat = new SimpleDateFormat(forMat);
try {
return dateFormat.format(date);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private Date getDate(String date,String forMat){
if (date == null) {
return null;
}
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(forMat);
return dateFormat.parse(date);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}