❶ 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条件