码迷,mamicode.com
首页 > 编程语言 > 详细

关于Java类Calendar做统计时 获取日期的一些常见操作

时间:2018-11-27 11:10:39      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:根据   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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!