java日期处理工具类
发布日期:2022-02-10 11:36:52 浏览次数:38 分类:技术文章

本文共 14887 字,大约阅读时间需要 49 分钟。

  1. import java.text.ParseException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. /** 
  9.  *  
  10.  * @author yli 
  11.  *  
  12.  */  
  13. public class DateUtil {  
  14.   
  15.     /** 
  16.      * 分隔符:空格 
  17.      */  
  18.     public static String SEPARATE_SPACE = " ";  
  19.   
  20.     /** 
  21.      * 分隔符:减号即 - 
  22.      */  
  23.     public static String SEPARATE_SUB = "-";  
  24.   
  25.     /** 
  26.      * 分隔符:空串 
  27.      */  
  28.     public static String SEPARATE_EMPTY = "";  
  29.   
  30.     /** 
  31.      * 日期第WW周格式模板 
  32.      */  
  33.     public static String TEMPLATE_WW = "ww";  
  34.   
  35.     /** 
  36.      * current_date 
  37.      */  
  38.     public static String CURRENT_DATE = "current_date";  
  39.   
  40.     /** 
  41.      * min_date 
  42.      */  
  43.     public static String MIN_DATE = "min_date";  
  44.   
  45.     /** 
  46.      * max_date 
  47.      */  
  48.     public static String MAX_DATE = "max_date";  
  49.   
  50.     /** 
  51.      * statis_date 
  52.      */  
  53.     public static String STATIS_DATE = "statis_date";  
  54.   
  55.     /** 
  56.      * statis_table 
  57.      */  
  58.     public static String STATIS_TABLE = "statis_table";  
  59.   
  60.     /** 
  61.      * select_date 
  62.      */  
  63.     public static String SELECT_DATE = "select_date";  
  64.   
  65.     /** 
  66.      * select_month 
  67.      */  
  68.     public static String SELECT_MONTH = "select_month";  
  69.   
  70.     /** 
  71.      * select_preMonth 
  72.      */  
  73.     public static String SELECT_PREMONTH = "select_preMonth";  
  74.   
  75.     /** 
  76.      * statis_month 
  77.      */  
  78.     public static String STATIS_MONTH = "statis_month";  
  79.   
  80.     /** 
  81.      * statis_preMonth 
  82.      */  
  83.     public static String STATIS_PREMONTH = "statis_preMonth";  
  84.   
  85.     /** 
  86.      * select_predate 
  87.      */  
  88.     public static String SELECT_PREDATE = "select_predate";  
  89.   
  90.     /** 
  91.      * statis_predate 
  92.      */  
  93.     public static String STATIS_PREDATE = "statis_predate";  
  94.   
  95.     /** 
  96.      * page_date 
  97.      */  
  98.     public static String PAGE_DATE = "page_date";  
  99.   
  100.     /** 
  101.      * select_date_d 
  102.      */  
  103.     public static String SELECT_DATE_D = "select_date_d";  
  104.   
  105.     /** 
  106.      * select_date_w 
  107.      */  
  108.     public static String SELECT_DATE_W = "select_date_w";  
  109.   
  110.     /** 
  111.      * select_date_m 
  112.      */  
  113.     public static String SELECT_DATE_M = "select_date_m";  
  114.   
  115.     /** 
  116.      * report_mode 
  117.      */  
  118.     public static String REPORT_MODE = "report_mode";  
  119.   
  120.     /** 
  121.      * 天的偏移量 
  122.      */  
  123.     public static String DAY_OFFSET = "dayOffset";  
  124.   
  125.     /** 
  126.      * 月的偏移量 
  127.      */  
  128.     public static String MONTH_OFFSET = "monthOffset";  
  129.   
  130.     /** 
  131.      * 年的偏移量 
  132.      */  
  133.     public static String YEAR_OFFSET = "yearOffset";  
  134.   
  135.     /** 
  136.      * 数据报使用的表名后缀 
  137.      *  
  138.      */  
  139.     public static enum ReportTableEnum {  
  140.         D("D"),   
  141.         W("W"),   
  142.         M("M"),   
  143.         D_7("D_7");  
  144.   
  145.         private final String value;  
  146.   
  147.         public String getValue() {  
  148.             return this.value;  
  149.         }  
  150.   
  151.         ReportTableEnum(String value) {  
  152.             this.value = value;  
  153.         }  
  154.     }  
  155.   
  156.     public static enum DateOffset {  
  157.         day_1(1), day_15(15), day_30(30), month_1(1);  
  158.   
  159.         private final int value;  
  160.   
  161.         public int getValue() {  
  162.             return this.value;  
  163.         }  
  164.   
  165.         DateOffset(int value) {  
  166.             this.value = value;  
  167.         }  
  168.     }  
  169.   
  170.     /** 
  171.      * 数据宝使用的日期格式,以下格式Java都支持<br> 
  172.      * 并且关于周的计算,和日期控件My97Date一样采用ISO8601定义的算法 
  173.      */  
  174.     public static enum DateFormatEnum {  
  175.   
  176.         /** 
  177.          * 20121208 
  178.          */  
  179.         yyyyMMdd("yyyyMMdd"),  
  180.   
  181.         /** 
  182.          * 2012-12-08 
  183.          */  
  184.         yyyyMMdd_sub("yyyy-MM-dd"),  
  185.   
  186.         /** 
  187.          * 2012年12月08日 
  188.          */  
  189.         yyyyMMdd_zh_cn("yyyy年MM月dd日"),  
  190.   
  191.         /** 
  192.          * 201249 
  193.          */  
  194.         yyyyww("yyyyww"),  
  195.   
  196.         /** 
  197.          * 2012-12-08 第49周 
  198.          */  
  199.         yyyyMMddww_sub("yyyy-MM-dd 第ww周"),  
  200.   
  201.         /** 
  202.          * 2012年12月08日 第49周 
  203.          */  
  204.         yyyyMMddww_zh_cn("yyyy年MM月dd日 第ww周"),  
  205.   
  206.         /** 
  207.          * 201212 
  208.          */  
  209.         yyyyMM("yyyyMM"),  
  210.   
  211.         /** 
  212.          * 2012-12 
  213.          */  
  214.         yyyyMM_sub("yyyy-MM"),  
  215.   
  216.         /** 
  217.          * 2012年12月 
  218.          */  
  219.         yyyyMM_zh_cn("yyyy年MM月");  
  220.   
  221.         private final String value;  
  222.   
  223.         public String getValue() {  
  224.             return value;  
  225.         }  
  226.   
  227.         DateFormatEnum(String value) {  
  228.             this.value = value;  
  229.         }  
  230.     }  
  231.   
  232.     /** 
  233.      * 报表模式[日/周/月/前7天/前15天/前30天/等等]<br> 
  234.      * 初始化为对应的数据库表名后缀:这样在SQL语句中不需要动态判断<br> 
  235.      */  
  236.     public static enum ReportMode {  
  237.         day("d"),  
  238.   
  239.         week("w"),  
  240.   
  241.         month("m"),  
  242.   
  243.         day7("d_7");  
  244.   
  245.         private final String value;  
  246.   
  247.         public String getValue() {  
  248.             return value;  
  249.         }  
  250.   
  251.         ReportMode(String value) {  
  252.             this.value = value;  
  253.         }  
  254.     }  
  255.   
  256.     public static Map<String, ReportMode> reportModeDict = new HashMap<String, ReportMode>();  
  257.     static {  
  258.         reportModeDict.put(ReportMode.day.toString(), ReportMode.day);  
  259.         reportModeDict.put(ReportMode.week.toString(), ReportMode.week);  
  260.         reportModeDict.put(ReportMode.month.toString(), ReportMode.month);  
  261.         reportModeDict.put(ReportMode.day7.toString(), ReportMode.day7);  
  262.     }  
  263.   
  264.     /** 
  265.      * 将用[户选择的日期格式][查询所需的日期格式][页面显示所需的日期格式]定义成字典 
  266.      */  
  267.     public static Map<ReportMode, DateFormatEnum[]> dateFormatDict = new HashMap<ReportMode, DateFormatEnum[]>();  
  268.   
  269.     /** 
  270.      * 完成已知报表模式下字典信息初始化<br> 
  271.      * 注意前XX天这种格式数据和日报没有区别 
  272.      */  
  273.     static {  
  274.         dateFormatDict.put(ReportMode.day, new DateFormatEnum[] {  
  275.                 DateFormatEnum.yyyyMMdd_sub, DateFormatEnum.yyyyMMdd,  
  276.                 DateFormatEnum.yyyyMMdd_zh_cn });  
  277.         dateFormatDict.put(ReportMode.week, new DateFormatEnum[] {  
  278.                 DateFormatEnum.yyyyMMddww_sub, DateFormatEnum.yyyyww,  
  279.                 DateFormatEnum.yyyyMMddww_zh_cn });  
  280.         dateFormatDict.put(ReportMode.month, new DateFormatEnum[] {  
  281.                 DateFormatEnum.yyyyMM_sub, DateFormatEnum.yyyyMM,  
  282.                 DateFormatEnum.yyyyMM_zh_cn });  
  283.         dateFormatDict.put(ReportMode.day7, new DateFormatEnum[] {  
  284.                 DateFormatEnum.yyyyMMdd_sub, DateFormatEnum.yyyyMMdd,  
  285.                 DateFormatEnum.yyyyMMdd_zh_cn });  
  286.     }  
  287.   
  288.     /** 
  289.      *  
  290.      * 日期格式转换:将日期转换成字符串 
  291.      *  
  292.      * @param date 
  293.      * @param dateFormat 
  294.      * @return 
  295.      */  
  296.     public static String formatDate(Date date, DateFormatEnum dateFormat) {  
  297.         SimpleDateFormat simpleFormat = new SimpleDateFormat(  
  298.                 dateFormat.getValue());  
  299.         return simpleFormat.format(date);  
  300.     }  
  301.   
  302.     /** 
  303.      *  
  304.      * 日期格式转换:将字符串转换成日期<br> 
  305.      * 如果date和格式dateFormat不匹配,将抛出异常,并返回null 
  306.      *  
  307.      * @param date 
  308.      * @param dateFormat 
  309.      * @return 
  310.      */  
  311.     public static Date formatDate(String date, DateFormatEnum dateFormat) {  
  312.         SimpleDateFormat simpleFormat = new SimpleDateFormat(  
  313.                 dateFormat.getValue());  
  314.         Date objectDate = null;  
  315.         try {  
  316.             objectDate = simpleFormat.parse(date);  
  317.         } catch (ParseException e) {  
  318.             e.printStackTrace();  
  319.         }  
  320.         return objectDate;  
  321.     }  
  322.   
  323.     // 根据天和年的偏移量计算最小和最大日期  
  324.     public static Map<String, Object> getMinMaxDate(int dayOffset,  
  325.             int yearOffset) {  
  326.         Date date = new Date();  
  327.         return getMinMaxDate(date, dayOffset, yearOffset);  
  328.     }  
  329.   
  330.     // 根据天和年的偏移量计算最小和最大日期  
  331.     public static Map<String, Object> getMinMaxDate(Date date, int dayOffset,  
  332.             int yearOffset) {  
  333.         Map<String, Object> minmaxMap = new HashMap<String, Object>();  
  334.   
  335.         // 设置日期  
  336.         Calendar calendar = Calendar.getInstance();  
  337.         calendar.setTime(date);  
  338.   
  339.         // 根据[天]的偏移量,以[date]为基准,计算日期控件所需的最大日期  
  340.         calendar.add(Calendar.DAY_OF_MONTH, dayOffset);  
  341.         String maxDateString = formatDate(calendar.getTime(),  
  342.                 DateFormatEnum.yyyyMMdd_sub);  
  343.   
  344.         // 根据[年]的偏移量,以[最大日期]为基准,计算日期控件所需的最小日期  
  345.         calendar.add(Calendar.YEAR, yearOffset);  
  346.         String minDateString = formatDate(calendar.getTime(),  
  347.                 DateFormatEnum.yyyyMMdd_sub);  
  348.   
  349.         minmaxMap.put(MIN_DATE, minDateString);  
  350.         minmaxMap.put(MAX_DATE, maxDateString);  
  351.   
  352.         return minmaxMap;  
  353.     }  
  354.   
  355.     /** 
  356.      * 根据用户指定的[报表模式+查询日期]获取查询所需的日期以及数据表名后缀<br> 
  357.      * 同时将日历控件在三种报表模式下需要的日期格式,以及页面所需的日期提示文本计算出来<br> 
  358.      *  
  359.      * @return 
  360.      */  
  361.     public static Map<String, Object> getStatisDateAndTable(  
  362.             Map<String, Object> params) {  
  363.   
  364.         if (null == params.get(REPORT_MODE)) {  
  365.             params.put(REPORT_MODE, ReportMode.day.toString());  
  366.         }  
  367.   
  368.         Calendar calendar = null;  
  369.   
  370.         // 如果没有指定查询日期,则以当前日期的前一天作为默认日期  
  371.         if (null == params.get(SELECT_DATE)) {  
  372.             calendar = Calendar.getInstance();  
  373.             calendar.add(Calendar.DAY_OF_MONTH, -1);  
  374.             params.put(SELECT_DATE,  
  375.                     formatDate(calendar.getTime(), DateFormatEnum.yyyyMMdd_sub));  
  376.         }  
  377.   
  378.         // 查询所需的统计日期  
  379.         String statisDate = null;  
  380.         // 数据表名后缀  
  381.         String statisTable = null;  
  382.         // 日历控件[天]日期  
  383.         String selectDate_D = null;  
  384.         // 日历控件[周]日期  
  385.         String selectDate_W = null;  
  386.         // 日历控件[月]日期  
  387.         String selectDate_M = null;  
  388.         // 页面日期提示信息  
  389.         String pageDateTip = null;  
  390.         // 用户指定的查询日期  
  391.         String selectDate = params.get(SELECT_DATE).toString();  
  392.         // 用户指定报表模式  
  393.         ReportMode reportMode = reportModeDict.get(params.get(REPORT_MODE).toString());  
  394.         // 获取各种日期格式  
  395.         DateFormatEnum[] dateFormatInfo = dateFormatDict.get(reportMode);  
  396.         // 原日期格式  
  397.         DateFormatEnum sourceDateFormat = dateFormatInfo[0];  
  398.         // 目标日期格式  
  399.         DateFormatEnum targetDateFormat = dateFormatInfo[1];  
  400.         // 页面日期格式  
  401.         DateFormatEnum pageDateFormat = dateFormatInfo[2];  
  402.   
  403.         // 报表模式的值即为表名后缀  
  404.         statisTable = reportMode.getValue().toUpperCase();  
  405.   
  406.         // 用户选择的日期:将字符转成Date  
  407.         Date sourceDate = formatDate(selectDate, sourceDateFormat);  
  408.   
  409.         // 获取数据库所需统计日期格式  
  410.         statisDate = formatDate(sourceDate, targetDateFormat);  
  411.   
  412.         // 获取页面日期提示所需日期格式  
  413.         pageDateTip = formatDate(sourceDate, pageDateFormat);  
  414.   
  415.         // 获取日历控件在三种报表模式下各自需要的值  
  416.         if (ReportMode.month.equals(reportMode)) {  
  417.             // 只有月报的日期无法转换成日报或周报  
  418.             calendar = Calendar.getInstance();  
  419.             calendar.add(Calendar.DAY_OF_MONTH, -1);  
  420.             sourceDate = calendar.getTime();  
  421.         }  
  422.         selectDate_D = formatDate(sourceDate, DateFormatEnum.yyyyMMdd_sub);  
  423.         selectDate_W = formatDate(sourceDate, DateFormatEnum.yyyyMMddww_sub);  
  424.         selectDate_M = formatDate(sourceDate, DateFormatEnum.yyyyMM_sub);  
  425.   
  426.         params.put(STATIS_DATE, statisDate);  
  427.         params.put(STATIS_TABLE, statisTable);  
  428.         params.put(PAGE_DATE, pageDateTip);  
  429.         params.put(SELECT_DATE_D, selectDate_D);  
  430.         params.put(SELECT_DATE_W, selectDate_W);  
  431.         params.put(SELECT_DATE_M, selectDate_M);  
  432.         return params;  
  433.     }  
  434.   
  435.     /** 
  436.      * 根据用户选择的日期获取相邻两个月日期数据<br> 
  437.      * 并将日期转换成查询所需的格式 
  438.      *  
  439.      * @param params 
  440.      * @return 
  441.      */  
  442.     public static Map<String, Object> getLinkedTwoMonth(  
  443.             Map<String, Object> params) {  
  444.         String statis_Date = null;  
  445.         String statis_preDate = null;  
  446.         String select_Date = null;  
  447.         String select_preDate = null;  
  448.         Calendar calendar = null;  
  449.   
  450.         // 如果月份数据为空,则以当前日期的前一天作为用户选择的月份数据  
  451.         if (null == params.get(SELECT_DATE)) {  
  452.             calendar = Calendar.getInstance();  
  453.             calendar.add(Calendar.DAY_OF_MONTH, -1);  
  454.             params.put(SELECT_DATE,  
  455.                     formatDate(calendar.getTime(), DateFormatEnum.yyyyMM_sub));  
  456.         }  
  457.   
  458.         // 根据用户选择的月份计算上一个月份日期  
  459.         select_Date = params.get(SELECT_DATE).toString();  
  460.         Date currDate = formatDate(select_Date, DateFormatEnum.yyyyMM_sub);  
  461.         statis_Date = formatDate(currDate, DateFormatEnum.yyyyMM);  
  462.   
  463.         calendar = Calendar.getInstance();  
  464.         calendar.setTime(currDate);  
  465.         calendar.add(Calendar.MONTH, -1);  
  466.         statis_preDate = formatDate(calendar.getTime(), DateFormatEnum.yyyyMM);  
  467.         select_preDate = formatDate(calendar.getTime(),  
  468.                 DateFormatEnum.yyyyMM_sub);  
  469.   
  470.         params.put(SELECT_PREDATE, select_preDate);  
  471.         params.put(STATIS_DATE, statis_Date);  
  472.         params.put(STATIS_PREDATE, statis_preDate);  
  473.         return params;  
  474.     }  
  475.   
  476.     /** 
  477.      * 将用户指定的查询开始于截至日期转换成查询所需的格式<br> 
  478.      * 如果查询截至日期为空,则取当前日期前一天作为默认截至日期 
  479.      *  
  480.      * @param params 
  481.      * @return 
  482.      */  
  483.     public static Map<String, Object> getLinkedTwoDate(  
  484.             Map<String, Object> params) {  
  485.         String statis_Date = null;  
  486.         String statis_preDate = null;  
  487.         String select_Date = null;  
  488.         String select_preDate = null;  
  489.   
  490.         // 如果查询的开始于截至日期为空,则默认取当前的前1天作为截至日期  
  491.         if (null == params.get(SELECT_DATE)  
  492.                 || null == params.get(SELECT_PREDATE)) {  
  493.             Calendar calendar = Calendar.getInstance();  
  494.             calendar.add(Calendar.DAY_OF_MONTH, -1);  
  495.             params.put(SELECT_DATE,  
  496.                     formatDate(calendar.getTime(), DateFormatEnum.yyyyMMdd_sub));  
  497.   
  498.             // 如果偏移量为空,则默认为15天  
  499.             int dayOffset = null == params.get(DAY_OFFSET) ? DateOffset.day_15  
  500.                     .getValue() : Integer.parseInt(params.get(DAY_OFFSET)  
  501.                     .toString());  
  502.             calendar.add(Calendar.DAY_OF_MONTH, -dayOffset);  
  503.             params.put(SELECT_PREDATE,  
  504.                     formatDate(calendar.getTime(), DateFormatEnum.yyyyMMdd_sub));  
  505.         }  
  506.   
  507.         // 将开始于截至日期格式转换成查询所需的格式  
  508.         select_Date = params.get(SELECT_DATE).toString();  
  509.         Date currDate = formatDate(select_Date, DateFormatEnum.yyyyMMdd_sub);  
  510.         statis_Date = formatDate(currDate, DateFormatEnum.yyyyMMdd);  
  511.   
  512.         select_preDate = params.get(SELECT_PREDATE).toString();  
  513.         currDate = formatDate(select_preDate, DateFormatEnum.yyyyMMdd_sub);  
  514.         statis_preDate = formatDate(currDate, DateFormatEnum.yyyyMMdd);  
  515.   
  516.         params.put(STATIS_DATE, statis_Date);  
  517.         params.put(STATIS_PREDATE, statis_preDate);  
  518.         return params;  
  519.     }  
  520.   
  521.     /** 
  522.      * 检查用户指定的查询时间是否有效<br> 
  523.      * 用户指定的查询时间只能在系统允许的最大时间和最小时间范围内 
  524.      *  
  525.      * @param params 
  526.      * @return 
  527.      */  
  528.     public static boolean isEffectiveDate(Map<String, Object> params) {  
  529.         if (null == params.get(STATIS_DATE) || null == params.get(MAX_DATE)  
  530.                 || null == params.get(MIN_DATE)) {  
  531.             return false;  
  532.         }  
  533.         ReportMode reportMode = ReportMode.day;  
  534.         if(null != params.get(REPORT_MODE)) {  
  535.             reportMode = reportModeDict.get(params.get(REPORT_MODE).toString());  
  536.         }  
  537.         // [日/周/月/前X天]的报表统一检查日期是否符合要求  
  538.         // 也包含只查询月报,且查询相邻两个月的数据,主要针对会员规模分析这种情况  
  539.         String statisDateStr = params.get(STATIS_DATE).toString();  
  540.         String maxDateStr = params.get(MAX_DATE).toString().replace(SEPARATE_SUB, SEPARATE_EMPTY);  
  541.         String minDateStr = params.get(MIN_DATE).toString().replace(SEPARATE_SUB, SEPARATE_EMPTY);  
  542.         if(ReportMode.month.equals(reportMode)) {  
  543.             statisDateStr += minDateStr.substring(6);   // 将最小日期最后的天数加到用户选择的月份数据上  
  544.         } else if(ReportMode.week.equals(reportMode)){  
  545.             statisDateStr = params.get(SELECT_DATE_D).toString().replace(SEPARATE_SUB, SEPARATE_EMPTY);  
  546.         }  
  547.         int statisDate = Integer.parseInt(statisDateStr);  
  548.         int maxDate = Integer.parseInt(maxDateStr);  
  549.         int minDate = Integer.parseInt(minDateStr);  
  550.         if(statisDate < minDate || statisDate > maxDate) {  
  551.             return false;  
  552.         }  
  553.           
  554.         // 处理以天为查询维度,且查询时间包含开始与截至时间的情况  
  555.         // 主要针对流量路径统计这种跨度只能限定在15天的报表  
  556.         if(null != params.get(STATIS_PREDATE) && !ReportMode.month.equals(reportMode)) {  
  557.             String statisPreDateStr = params.get(STATIS_PREDATE).toString();  
  558.             int statisPreDate = Integer.parseInt(statisPreDateStr);  
  559.             // 查询开始日期不能大于截至日期  
  560.             if(statisPreDate > statisDate) {  
  561.                 return false;  
  562.             }  
  563.             // 比较查询开始于截至日期之间跨度是否有效  
  564.             int dayOffset = null == params.get(DAY_OFFSET) ? DateOffset.day_15.getValue()   
  565.                     : Integer.parseInt(params.get(DAY_OFFSET).toString());  
  566.             Date statisDateObj = formatDate(statisDateStr, DateFormatEnum.yyyyMMdd);  
  567.             Calendar calendar = Calendar.getInstance();  
  568.             calendar.setTime(statisDateObj);  
  569.             calendar.add(Calendar.DAY_OF_MONTH, -dayOffset);  
  570.             String legalPreDateStr = formatDate(calendar.getTime(), DateFormatEnum.yyyyMMdd);  
  571.             int legalPreDate = Integer.parseInt(legalPreDateStr);  
  572.             // 如果查询的开始日期小于有效的日期则不合法  
  573.             if(statisPreDate < legalPreDate) {  
  574.                 return false;  
  575.             }  
  576.         }  
  577.           
  578.         return true;  
  579.     }  
  580.   
  581.     public static void main(String[] args) {  
  582.         /****** 根据偏移量计算最小最大日期 ***********/  
  583.         // 指定日期  
  584.         // Calendar cal = Calendar.getInstance();  
  585.         // cal.set(2010, 9, 18); // 2010-10-18 注意月份  
  586.         // System.out.println(getMinMaxDate(cal.getTime(), -1, -2));  
  587.   
  588.         // 不指定日期,则默认取当前日期  
  589.         Map<String, Object> minMaxMap = getMinMaxDate(-1, -1);  
  590.         System.out.println(minMaxMap);  
  591.         /****************************/  
  592.           
  593.         /********** 根据用户指定的[查询日期]和[报表模式]计算SQL所需的统计日期 *************/  
  594.         Map<String, Object> params = new HashMap<String, Object>();  
  595.         params.putAll(minMaxMap);  
  596.         params.put(REPORT_MODE, ReportMode.week.toString());  
  597.         params.put(SELECT_DATE, "2012-04-06 第14周");  
  598.         getStatisDateAndTable(params);  
  599.         System.out.println(params);  
  600.         // 日期是否合法  
  601.         System.out.println(isEffectiveDate(params));  
  602.           
  603.           
  604.   
  605.         params.put(REPORT_MODE, ReportMode.month.toString());  
  606.         params.put(SELECT_DATE, "2012-03");  
  607.         getStatisDateAndTable(params);  
  608.         System.out.println(params);  
  609.         // 日期是否合法  
  610.         System.out.println(isEffectiveDate(params));  
  611.   
  612.         params.put(REPORT_MODE, ReportMode.day.toString());  
  613.         params.put(SELECT_DATE, "2013-04-06");  
  614.         getStatisDateAndTable(params);  
  615.         System.out.println(params);  
  616.         // 日期是否合法  
  617.         System.out.println(isEffectiveDate(params));  
  618.           
  619.         params.put(REPORT_MODE, ReportMode.day7.toString());  
  620.         params.put(SELECT_DATE, "2013-04-05");  
  621.         getStatisDateAndTable(params);  
  622.         System.out.println(params);  
  623.         // 日期是否合法  
  624.         System.out.println(isEffectiveDate(params));  
  625.   
  626.         // 不指定日期则取当前日期前一天作为统计日期  
  627.         params.remove(REPORT_MODE);  
  628.         params.remove(SELECT_DATE);  
  629.         getStatisDateAndTable(params);  
  630.         System.out.println(params);  
  631.         // 日期是否合法  
  632.         System.out.println(isEffectiveDate(params));  
  633.   
  634.         // 对于只有月报,且是查询相邻两个月的数据  
  635.         // 只需要计算两个月份日期即可  
  636.         Map<String, Object> params1 = new HashMap<String, Object>();  
  637.         params1.putAll(minMaxMap);  
  638.         params1.put(REPORT_MODE, ReportMode.month.toString());  
  639.         params1.put(SELECT_DATE, "2013-04");  
  640.         getLinkedTwoMonth(params1);  
  641.         System.out.println(params1);  
  642.         // 日期是否合法  
  643.         System.out.println(isEffectiveDate(params1));  
  644.   
  645.         // 对于只有日报,且是查询相邻不超过15天的数据  
  646.         Map<String, Object> params2 = new HashMap<String, Object>();  
  647.         params2.putAll(minMaxMap);  
  648.         params2.put(SELECT_DATE, "2013-01-28");  
  649.         params2.put(SELECT_PREDATE, "2013-01-22");  
  650.         getLinkedTwoDate(params2);  
  651.         System.out.println(params2);  
  652.         // 日期是否合法  
  653.         System.out.println(isEffectiveDate(params2));  
  654.     }  

转载地址:https://blog.csdn.net/courage89/article/details/8888724 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:JAVA处理日期时间常用方法: java.util.Calendar
下一篇:InputStream 转 String

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月20日 14时40分28秒