标签:根据 today date() bsp block 定时 todo mon simple
1、获取本周的周一到周日
//通过调整日历,获得本天所属周的周一和周日
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal=new GregorianCalendar(); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setTime(new Date()); cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); String startTime1 = sdf.format(cal.getTime()); cal.set(Calendar.DATE, cal.get(cal.DATE) + 6); String endTime1 = sdf.format(cal.getTime()); date = startTime1+"~"+endTime1;
2、根据本周的时间获得上周的周一和周日Calendar calendar = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTime(startTime);//startTime当前查询周的周一(不一定是本周,也可以是指定查询某一天的周一)
calendar.set(Calendar.DATE, calendar.get(calendar.DATE) -1);
end2 = sdf.parse(sdf.format(calendar.getTime()));
calendar.set(Calendar.DATE, calendar.get(calendar.DATE) -6);
start2 = sdf.parse(sdf.format(calendar.getTime()));
3、按照查询的某周的周一到周日获取指定周的周数据
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //遍历日期来查询,保证一天一查,查询结果是对的 Calendar c = Calendar.getInstance(); c.setTime(startTime); Date time = sdf.parse(sdf.format(c.getTime())); while(time.getTime()<=endTime.getTime()) { //不是今天 if(time.getTime()!=sdf.parse(sdf.format(new Date())).getTime()) { SalesOrderStatDay dayObj = salesOrderStatDayDAO.getDataByDate(userId,time); list.add(dayObj); } else { //统计一下今天的报单数据,因为后台数据是使用quartz定时任务在零点才去统计当天数据,为了能看到当天数据 只能去实时查询 SalesOrderStatDay dayObj = getTodayData(userId); list.add(dayObj); } c.add(Calendar.DATE, 1); time = sdf.parse(sdf.format(c.getTime()));//从周一加到周日 }
4、获取指定月份
sdf= new SimpleDateFormat("yyyy-MM"); Calendar calendar = Calendar.getInstance(); try { calendar.setTime(sdf.parse(month)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } calendar.set(calendar.MONTH, calendar.get(calendar.MONTH) -1);//进行减一操作,因为系统计算月份是从0开始 date2 = sdf.format(calendar.getTime());
5、按照月份查询每一天数据
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM"); SimpleDateFormat sdflll= new SimpleDateFormat("yyyy-MM-dd"); Date today = sdflll.parse(sdflll.format(new Date())); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(date)); //总天数 int dayNum = cal.getActualMaximum(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH,1); for(int i=1;i<=dayNum;i++) { Date time = sdflll.parse(sdflll.format(cal.getTime())); //不是今天 if(cal.getTime()!=today.getTime()) { SalesOrderStatDay dayObj = salesOrderStatDayDAO.getDataByDate(userId,time); list.add(dayObj); } else { //统计一下今天的报单数据 SalesOrderStatDay dayObj = getTodayData(userId); list.add(dayObj); } cal.add(Calendar.DATE, 1);//日期加一 }
关于Java类Calendar做统计时 获取日期的一些常见操作
标签:根据 today date() bsp block 定时 todo mon simple
原文地址:https://www.cnblogs.com/lxk233/p/10024751.html