获取时间段的工具类(一般用于匹配数据库查询)

/**
     * 获取指定日期的工具方法
     */
    public Map<String,Object> getStartAndEndTime(String timeType){
        String startTime="";
        String endTime="";
        Calendar cal=Calendar.getInstance();
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String todayDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        switch (timeType) {
            case "1"://今日
                startTime=today+" 00:00:00";
                endTime=today+" 23:59:59";
                break;
            case "2"://昨日
                cal.add(Calendar.DATE,-1);
                String yesToday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
                startTime=yesToday+" 00:00:00";
                endTime=yesToday+" 23:59:59";
                break;
            case "3"://近7天
                cal.add(Calendar.DATE,-6);
                String sevenDay = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
                startTime=sevenDay+" 00:00:00";
                endTime=today+" 23:59:59";
                break;
            case "4"://近30天
                cal.add(Calendar.DATE,-29);
                String thirtyDay = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
                startTime=thirtyDay+" 00:00:00";
                endTime=today+" 23:59:59";
                break;
            case "5"://本月
                cal.add(Calendar.MONTH, 0);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                String thisMonthFirstDay = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
                startTime=thisMonthFirstDay+" 00:00:00";
                endTime=today+" 23:59:59";
                break;
            case "6"://本月截止当前时间
                cal.add(Calendar.MONTH, 0);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                String thisMonthFirstDay1 = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
                startTime=thisMonthFirstDay1+" 00:00:00";
                endTime=todayDate;
                break;
            case "7"://上月
                cal.set(Calendar.DAY_OF_MONTH, 0);
                String lastMonthEndtDay = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
                cal.set(Calendar.DAY_OF_MONTH, 1);
                String lastMonthFirstDay = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
                startTime=lastMonthFirstDay+" 00:00:00";
                endTime=lastMonthEndtDay+" 23:59:59";
                break;
            default:
                startTime=null;
                endTime=null;
                break;
        }
        Map<String,Object> turnMap=new HashMap<String,Object>();
        turnMap.put("startTime",startTime);
        turnMap.put("endTime",endTime);
        return turnMap;
    }
posted @ 2021-04-23 15:05  初出茅庐小菜鸡  阅读(96)  评论(0编辑  收藏  举报