日期:2014-05-19 浏览次数:20787 次
private static void printYearMonth(String start, String end) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date s = sdf.parse(start); Date e = sdf.parse(end); Calendar cals = Calendar.getInstance(); cals.setTime(s); Calendar cale = Calendar.getInstance(); cale.setTime(e); StringBuffer sb = new StringBuffer(); while (cals.before(cale)) { sb.append(sdf.format(cals.getTime())); sb.append("至"); cals.set(Calendar.DATE, 1); cals.add(Calendar.MONTH, 1); if (cals.after(cale)) { cals = cale; sb.append(sdf.format(cals.getTime()) + "\n"); break; } cals.add(Calendar.DATE, -1); sb.append(sdf.format(cals.getTime()) + "\n"); cals.add(Calendar.DATE, 1); } System.out.print(sb.toString()); }