❶ java給定兩個日期,求兩個日期之間的日期段,以月為分隔~!
用java的日歷類
public static void main(String[] args) throws ParseException {
String d1= "2011-05-12";
String d2="2011-08-15";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = format.parse(d1);
Date date2 = format.parse(d2);
Calendar cal =Calendar.getInstance();
cal.setTime(date1);
int day = cal.get(Calendar.DAY_OF_MONTH);
while(cal.getTime().before(date2)){
String begin = format.format(cal.getTime());
int max = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, max);
String end = cal.getTime().before(date2) ? format.format(cal.getTime()) : d2;
System.out.println(begin+"~"+end);
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, day);
}
}
❷ java 按日期分組 演算法
直接用SQL語句
select count(id) as 數量,day(date) as 生產日期 from table group by day(date) order by day(czrq)
如果庫里不止一個月的數據,那麼再加個where條件