zoukankan      html  css  js  c++  java
  • 获取时间段的工具类(一般用于匹配数据库查询)

    /**
         * 获取指定日期的工具方法
         */
        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;
        }
    
  • 相关阅读:
    基础数据类型转换和深浅拷贝
    文件操作
    运算符相关
    基础数据类型概述
    dijkstra算法学习笔记
    洛谷 P1880 [NOI1995]石子合并
    最大公约数GCD学习笔记
    洛谷 P1280 尼克的任务
    [NOIp2012]疫情控制
    [NOIp2016]蚯蚓
  • 原文地址:https://www.cnblogs.com/wtao0730/p/14693904.html
Copyright © 2011-2022 走看看