我的日期为“2015年1月1日”,日期为“2015年4月1日”.我想找出fromDate和toDate之间的季度数.我的代码就像
String fromDate = "01-Jan-2015";
String toDate = "01-Apr-2015";
ArrayList quarters = new ArrayList();
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
DateFormat dfYY = new SimpleDateFormat("yy");
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(fromDate));
Calendar cal1 = Calendar.getInstance();
cal1.setTime(df.parse(toDate));
while (cal1.getTime().after(cal.getTime())) {
int month = cal.get(Calendar.MONTH) + 1;
int quarter = month % 3 == 0 ? (month / 3) : (month / 3) + 1;
quarters.add("Q" + quarter + "-" + dfYY.format(cal.getTime()));
cal.add(Calendar.MONTH, 3);
}
但它的产量为Q1-15,Q2-15缺失.如果我输入日期为“02-Apr-2015”,那么它将给出