@Test public void dateTest() throws Exception{ String newDate ="2015-11-30"; String dedate ="2014-11-17"; SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM"); Calendar calendarNew = Calendar.getInstance(); calendarNew.setTime(sd.parse(newDate));//页面传进来日期 Calendar calendarNew1 = Calendar.getInstance(); calendarNew1.setTime(sd.parse(dedate));//首次申请日期 int loanDay =Integer.valueOf(df.parse(newDate).getDate()); //取到日 while (!calendarNew1.getTime().after(calendarNew.getTime())) { calendarNew1.add(Calendar.MONTH,1); int days2 = calendarNew1.getActualMaximum(Calendar.DAY_OF_MONTH); //当前月总天数 if (loanDay > days2) { calendarNew1.set(Calendar.DAY_OF_MONTH, days2); //set月的最大天数 } else { calendarNew1.set(Calendar.DAY_OF_MONTH, loanDay); //set申请日 } System.out.println(df.format(calendarNew1.getTime())); } }