时间DateUtil的共通方法总结

在做图表的检索时,经常会用到检索的时间条件,时间格式的不同,以及间隔不同。因此总结了一套

[java]  view plain  copy
  1. /*** 
  2. *获得本月第一天 
  3. ****/  
  4.    public static String getMonthFirstDay() {  
  5.   
  6.        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  7.        Calendar c = Calendar.getInstance();  
  8.        c.add(Calendar.MONTH, 0);  
  9.        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天  
  10.        String first = format.format(c.getTime());  
  11.        return first;  
  12.    }  
  13.   
  14. /*** 
  15. *获得本月最后一天 
  16. ****/  
  17.    public static String getMonthyLastDay() {  
  18.   
  19.        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  20.        Calendar ca = Calendar.getInstance();  
  21.        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
  22.        String last = format.format(ca.getTime());  
  23.        return last;  
  24.    }  
  25.   
  26.    /**** 
  27.     *  
  28.     * 获得时间往前推1年 
  29.     * ***/  
  30.    public static String getLastYear() {  
  31.   
  32.        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  33.        Calendar ca = Calendar.getInstance();  
  34.        ca.set(Calendar.YEAR, -1);  
  35.        String last = format.format(ca.getTime());  
  36.        return last;  
  37.    }  
  38.   
  39.    /** 
  40.     * yyyy-MM-dd 
  41.     */  
  42.    public static final String YEAR_TO_DAY = "yyyy-MM-dd";  
  43.   
  44.    /** 
  45.     * yyyy-MM-dd HH:mm:ss 
  46.     */  
  47.    public static final String YEAR_TO_SECOND = "yyyy-MM-dd HH:mm:ss";  
  48.   
  49.    public static String formatDate(String format) {  
  50.   
  51.        SimpleDateFormat sdf = new SimpleDateFormat(format);  
  52.        return sdf.format(new Date());  
  53.    }  
  54.   
  55.    public static String formatDate(Date date, String format) {  
  56.   
  57.        SimpleDateFormat sdf = new SimpleDateFormat(format);  
  58.        return sdf.format(date);  
  59.    }  
  60.   
  61.    public static String formatTomorrowDate(String format) {  
  62.   
  63.        SimpleDateFormat sdf = new SimpleDateFormat(format);  
  64.        Date date = new Date();  
  65.        Calendar calendar = Calendar.getInstance();  
  66.        calendar.setTime(date);  
  67.        calendar.add(Calendar.DAY_OF_MONTH, 1);  
  68.        return sdf.format(calendar.getTime());  
  69.    }  
  70.   
  71.    public static int compare(Date date1, Date date2) {  
  72.   
  73.        return date1.compareTo(date2);  
  74.    }  
  75.   
  76.    public static int compare(String date1, String format, Date date2) throws ParseException {  
  77.   
  78.        SimpleDateFormat sdf = new SimpleDateFormat(format);  
  79.        Date date = sdf.parse(date1);  
  80.        return date.compareTo(date2);  
  81.    }  
  82.   
  83.    public static Date parse(String date, String format) throws ParseException {  
  84.   
  85.        SimpleDateFormat sdf = new SimpleDateFormat(format);  
  86.        return sdf.parse(date);  
  87.    }  
  88.   
  89.    public static Date parse(String date, String format, Locale locale, TimeZone timeZone) throws ParseException {  
  90.   
  91.        SimpleDateFormat sdf = new SimpleDateFormat(format, locale);  
  92.        sdf.setTimeZone(timeZone);  
  93.        return sdf.parse(date);  
  94.    }  
  95.   
  96.    public static Date getNextDate(String hourToSecond) throws ParseException {  
  97.   
  98.        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_TO_DAY);  
  99.        Date date = sdf.parse(DateUtil.formatDate(YEAR_TO_DAY) + " " + hourToSecond);  
  100.        return date;  
  101.    }  
  102.   
  103.    public static Date getCurrentDateRandomSecond(String hourToSecond) throws ParseException {  
  104.   
  105.        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_TO_SECOND);  
  106.        String[] fields = hourToSecond.split("\\:");  
  107.        Integer randomSecond = RandomUtils.nextInt(59);  
  108.        String second = (randomSecond.toString().length() == 1 ? "0" + randomSecond : randomSecond + "");  
  109.        String date = DateUtil.formatDate(YEAR_TO_DAY) + " " + fields[0] + ":" + fields[1] + ":" + second;  
  110.        return sdf.parse(date);  
  111.    }  
  112.   
  113.    public static Date getNextDateRandomSecond(String hourToSecond) throws ParseException {  
  114.   
  115.        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_TO_SECOND);  
  116.        String[] fields = hourToSecond.split("\\:");  
  117.        Integer randomSecond = RandomUtils.nextInt(59);  
  118.        String second = (randomSecond.toString().length() == 1 ? "0" + randomSecond : randomSecond + "");  
  119.        String date = DateUtil.formatTomorrowDate(YEAR_TO_DAY) + " " + fields[0] + ":" + fields[1] + ":" + second;  
  120.        return sdf.parse(date);  
  121.    }  
  122.   
  123.    public static String formatDateCHS(Date date) {  
  124.   
  125.        Calendar calendar = new GregorianCalendar();  
  126.        calendar.setTime(date);  
  127.        StringBuilder str = new StringBuilder();  
  128.        str.append(calendar.get(Calendar.YEAR));  
  129.        str.append("年");  
  130.        str.append(calendar.get(Calendar.MONTH) + 1);  
  131.        str.append("月");  
  132.        str.append(calendar.get(Calendar.DAY_OF_MONTH));  
  133.        str.append("日");  
  134.        return str.toString();  
  135.    }  
  136.   
  137.    /** 
  138.     * 获取之后的?天 
  139.     * 
  140.     * @param today 传入的当前天 
  141.     * @param i     需要相加的天数 
  142.     * @return 
  143.     */  
  144.    @SuppressWarnings("static-access")  
  145.    public static String getNextDay(String today, int i) {  
  146.   
  147.        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  148.        Date dd = new Date();  
  149.        try {  
  150.            dd = formatter.parse(today);  
  151.        } catch (ParseException e) {  
  152.            e.printStackTrace();  
  153.        }  
  154.        Calendar calendar = new GregorianCalendar();  
  155.        calendar.setTime(dd);  
  156.        calendar.add(calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动  
  157.        dd = calendar.getTime(); // 这个时间就是日期往后推一天的结果  
  158.        String dateString = formatter.format(dd);  
  159.        return dateString;  
  160.    }  
  161.   
  162.    /** 
  163.     * 获取传入参数之后的?天 
  164.     * 
  165.     * @param today 传入的当前天 
  166.     * @param i     需要相加的天数 
  167.     * @param YYYY/MM/DD 
  168.     * @return 
  169.     */  
  170.    @SuppressWarnings("static-access")  
  171.    public static String getBeforeDay(String today, int i) {  
  172.   
  173.        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");  
  174.        Date dd = new Date();  
  175.        try {  
  176.            dd = formatter.parse(today);  
  177.        } catch (ParseException e) {  
  178.            e.printStackTrace();  
  179.        }  
  180.        Calendar calendar = new GregorianCalendar();  
  181.        calendar.setTime(dd);  
  182.        calendar.add(calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动  
  183.        dd = calendar.getTime(); // 这个时间就是日期往后推一天的结果  
  184.        String dateString = formatter.format(dd);  
  185.        return dateString;  
  186.    }  
  187.   
  188.    /** 
  189.     * 获取传入参数之后的?年 
  190.     * 
  191.     * @param today 传入的当前天 
  192.     * @param i     需要相加年数 
  193.     * @return 
  194.     */  
  195.    @SuppressWarnings("static-access")  
  196.    public static String getNextYear(String today, int i) {  
  197.   
  198.        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  199.        Date dd = new Date();  
  200.        try {  
  201.            dd = formatter.parse(today);  
  202.        } catch (ParseException e) {  
  203.            e.printStackTrace();  
  204.        }  
  205.        Calendar calendar = new GregorianCalendar();  
  206.        calendar.setTime(dd);  
  207.        calendar.add(calendar.YEAR, i);// 把日期往后增加一年.整数往后推,负数往前移动  
  208.        dd = calendar.getTime(); // 这个时间就是日期往后推一年的结果  
  209.        String dateString = formatter.format(dd);  
  210.        return dateString;  
  211.    }  
  212.   
  213.    /** 
  214.     * 获取传入参数之后的?年 
  215.     * 
  216.     * @param today 传入的当前天 
  217.     * @param i     需要相加年数 
  218.     * @return 
  219.     */  
  220.    @SuppressWarnings("static-access")  
  221.    public static Date getNextYearOnDate(String today, int i) {  
  222.   
  223.        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  224.        Date dd = new Date();  
  225.        try {  
  226.            dd = formatter.parse(today);  
  227.        } catch (ParseException e) {  
  228.            e.printStackTrace();  
  229.        }  
  230.        Calendar calendar = new GregorianCalendar();  
  231.        calendar.setTime(dd);  
  232.        calendar.add(calendar.YEAR, i);// 把日期往后增加一年.整数往后推,负数往前移动  
  233.        dd = calendar.getTime(); // 这个时间就是日期往后推一天的结果  
  234.        return dd;  
  235.    }  
  236.   
  237.    /** 
  238.     * 获取传入参数之后的?天 
  239.     * 
  240.     * @param today 传入的当前天 
  241.     * @param i     需要相加的天数 
  242.     * @return 
  243.     */  
  244.    @SuppressWarnings("static-access")  
  245.    public static Date getNextDayonDate(String today, int i) {  
  246.   
  247.        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  248.        Date dd = new Date();  
  249.        try {  
  250.            dd = formatter.parse(today);  
  251.        } catch (ParseException e) {  
  252.            e.printStackTrace();  
  253.        }  
  254.        Calendar calendar = new GregorianCalendar();  
  255.        calendar.setTime(dd);  
  256.        calendar.add(calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动  
  257.        dd = calendar.getTime(); // 这个时间就是日期往后推一天的结果  
  258.        return dd;  
  259.    }  
  260.   
  261.    public static Date getNextDay(Date date, int i) {  
  262.   
  263.        Calendar calendar = Calendar.getInstance();  
  264.        calendar.setTime(date);  
  265.        calendar.add(Calendar.DAY_OF_MONTH, i);  
  266.        date = calendar.getTime();  
  267.        return date;  
  268.    }  
  269.   
  270.    public static Date getNextYear(Date date, int i) {  
  271.   
  272.        Calendar calendar = Calendar.getInstance();  
  273.        calendar.setTime(date);  
  274.        calendar.add(Calendar.YEAR, i);  
  275.        date = calendar.getTime();  
  276.        return date;  
  277.    }  
  278.   
  279.    /** 
  280.     * 获取指定周期之前的时刻 
  281.     * 
  282.     * @param endTime 
  283.     * @param period 
  284.     * @return 
  285.     */  
  286.    public static long getBeforPeriodTimestamp(long endTime, DateUtil.Period period) {  
  287.   
  288.        Calendar calendar = Calendar.getInstance();  
  289.        calendar.setTimeInMillis(endTime);  
  290.   
  291.        calendar.add(period.getCalendarField(), 0 - period.getAmount());  
  292.        return calendar.getTimeInMillis();  
  293.    }  
  294.   
  295.    ;  
  296.   
  297.    /** 
  298.     * 获取指定周期之前的时长 
  299.     * 
  300.     * @param endTime 
  301.     * @param period 
  302.     * @return 
  303.     */  
  304.    public static long getBeforPeriodDuration(long endTime, DateUtil.Period period) {  
  305.   
  306.        Calendar calendar = Calendar.getInstance();  
  307.        calendar.setTimeInMillis(endTime);  
  308.   
  309.        calendar.add(period.getCalendarField(), 0 - period.getAmount());  
  310.        return endTime - calendar.getTimeInMillis();  
  311.    }  
  312.   
  313.    ;  
  314.   
  315.    public enum Period {  
  316.        /** 
  317.         * 一天 
  318.         */  
  319.        ONE_DAY("一天", Calendar.DAY_OF_MONTH, 1),  
  320.        /** 
  321.         * 一天 
  322.         */  
  323.        TWO_DAY("两天", Calendar.DAY_OF_MONTH, 2),  
  324.        /** 
  325.         * 一天 
  326.         */  
  327.        THREE_DAY("三天", Calendar.DAY_OF_MONTH, 3),  
  328.        /** 
  329.         * 一天 
  330.         */  
  331.        ONE_WEEK("一星期", Calendar.WEEK_OF_MONTH, 1),  
  332.        /** 
  333.         * 一天 
  334.         */  
  335.        TWO_WEEK("两星期", Calendar.WEEK_OF_MONTH, 2),  
  336.        /** 
  337.         * 一天 
  338.         */  
  339.        THREE_WEEK("三星期", Calendar.WEEK_OF_MONTH, 3),  
  340.        /** 
  341.         * 一天 
  342.         */  
  343.        ONE_MONTH("一月", Calendar.MONTH, 1);  
  344.   
  345.        private Period(String name, int calendarField, int amount) {  
  346.            this.name = name;  
  347.            this.calendarField = calendarField;  
  348.            this.amount = amount;  
  349.        }  
  350.   
  351.        private String name;  
  352.   
  353.        private int calendarField;  
  354.   
  355.        private int amount;  
  356.   
  357.        public String getName() {  
  358.   
  359.            return this.name;  
  360.        }  
  361.   
  362.        public int getCalendarField() {  
  363.   
  364.            return calendarField;  
  365.        }  
  366.   
  367.        public int getAmount() {  
  368.   
  369.            return amount;  
  370.        }  
  371.    }  
  372.   
  373.    /** 
  374.     * @param time 
  375.     * @param format 
  376.     * @return 
  377.     */  
  378.    public static String format(Long time, String format) {  
  379.   
  380.        Calendar calendar = Calendar.getInstance();  
  381.        calendar.setTimeInMillis(time);  
  382.        return DateUtil.formatDate(calendar.getTime(), format);  
  383.    }  
  384.   
  385.    public static boolean isWorkDay(Calendar calendar) {  
  386.   
  387.        return !(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY  
  388.                || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);  
  389.    }  
  390.   
  391.    /** 
  392.     * @param date 
  393.     * @return 
  394.     */  
  395.    public static boolean isWorkDay(Date date) {  
  396.   
  397.        Calendar c = Calendar.getInstance();  
  398.        c.setTime(date);  
  399.        return isWorkDay(c);  
  400.    }  
  401.   
  402.    /** 
  403.     * @param year 
  404.     * @return 
  405.     */  
  406.    public static final boolean isGregorianLeapYear(int year) {  
  407.   
  408.        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);  
  409.    }  
  410.   
  411.    /** 
  412.     * @param mon 
  413.     * @return 
  414.     */  
  415.    public static final String addMonth(int mon) {  
  416.   
  417.        return mon + 1 < 10 ? "0" + (mon + 1) : (mon + 1) + "";  
  418.    }  
  419.   
  420.    /** 
  421.     * @param day 
  422.     * @return 
  423.     */  
  424.    public static final String addDay(int day) {  
  425.   
  426.        return day < 10 ? "0" + day : day + "";  
  427.    }  
  428.   
  429.    /** 
  430.     * getDateTimeTicks(这里用一句话描述这个方法的作用) 
  431.     * java时间转C#时间 
  432.     * 
  433.     * @param @return 设定文件 
  434.     * @return String    DOM对象 
  435.     * @Exception 异常对象 
  436.     * @since CodingExample Ver(编码范例查看) 1.1 
  437.     */  
  438.    public static long getDateTimeTicks() {  
  439.   
  440.        //C# DateTime.Now.Ticks属性的值表示自 0001 年 1 月 1 日午夜 12:00:00(表示 DateTime.MinValue)以来经过的以 100 纳秒为间隔的间隔数  
  441.        long timeticks;  
  442.        //1979与0001相隔毫秒数 (1979-0001)*365*24*60*60*1000  
  443.        long time_JAVA_Long = Long.parseLong("62378208000000");  
  444.        //java长整型日期,毫秒为单位              
  445.        Date dt_1979 = new Date();  
  446.        //long型的毫秒数   
  447.        long tricks_1979 = dt_1979.getTime();  
  448.        //1970年1月1日刻度           
  449.        long time_tricks = (tricks_1979 + time_JAVA_Long) * 10000;  
  450.        timeticks = time_tricks;  
  451.   
  452.        return timeticks;  
  453.    }  
  454.   
  455.    /** 
  456.     * getDayCountOfMonth 
  457.     * 取某年某月的天數 
  458.     * 
  459.     * @param date 日期YYYYMMDD 
  460.     * @return int 月的天數 
  461.     * @Exception 异常对象 
  462.     */  
  463.    public static int getDayCountOfMonth(String date) {  
  464.   
  465.        try {  
  466.            if (StringUtil.isEmpty(date)) {  
  467.                return 0;  
  468.            }  
  469.            switch (date.substring(46)) {  
  470.                case "01":  
  471.                case "03":  
  472.                case "05":  
  473.                case "07":  
  474.                case "08":  
  475.                case "10":  
  476.                case "12":  
  477.                    return 31;  
  478.                case "04":  
  479.                case "06":  
  480.                case "09":  
  481.                case "11":  
  482.                    return 30;  
  483.                case "02":  
  484.                    //润年  
  485.                    if (isGregorianLeapYear(Integer.parseInt(date.substring(04)))) {  
  486.                        return 29;  
  487.                    } else {  
  488.                        return 28;  
  489.                    }  
  490.            }  
  491.   
  492.        } catch (Exception e) {  
  493.            e.printStackTrace();  
  494.        }  
  495.        return 0;  
  496.    }  

第二个:

[java]  view plain  copy
  1. TimeUtils 实体类共通的方法  
  2.   
  3.     /** 
  4.      * 获取前n天日期和相应星期数 
  5.      * 
  6.      * @return 日期和相应星期数 
  7.      */  
  8.     public static Map<String, String> getweektime(String d) {  
  9.   
  10.         Map<String, String> retMap = new HashMap<String, String>();  
  11.         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
  12.         Calendar c = Calendar.getInstance();  
  13.         c.add(Calendar.DATE, -Integer.parseInt(d));  
  14.         Date monday = c.getTime();  
  15.         String week = getWeekOfDate(monday);  
  16.         String preMonday = sdf.format(monday);  
  17.         retMap.put("week", week);  
  18.         retMap.put("time", preMonday);  
  19.   
  20.         SimpleDateFormat MM = new SimpleDateFormat("M");  
  21.         SimpleDateFormat dd = new SimpleDateFormat("dd");  
  22.         String MM1 = MM.format(monday);  
  23.         String dd1 = dd.format(monday);  
  24.         String res = MM1 + "月" + dd1 + "日";  
  25.         retMap.put("monDay", res);  
  26.   
  27.         return retMap;  
  28.     }  
  29.   
  30.     /** 
  31.      * 获取当前日期是星期几 
  32.      * 
  33.      *  
  34.      */  
  35.     public static String getWeekOfDate(Date dt) {  
  36.   
  37.         String[] weekDays = { "星期日""星期一""星期二""星期三""星期四""星期五""星期六" };  
  38.         Calendar cal = Calendar.getInstance();  
  39.         cal.setTime(dt);  
  40.         int w = cal.get(Calendar.DAY_OF_WEEK) - 1;  
  41.         if (w < 0)  
  42.             w = 0;  
  43.         return weekDays[w];  
  44.     }  
  45.   
  46.     /** 
  47.      * 日期格式化为xxxx年xx月xx日  xx:xx:xx 
  48.      * 
  49.      * @param date 
  50.      */  
  51.     public static String formmaterDateTimeStringToString(String datetime) {  
  52.   
  53.         String result = "";  
  54.         if (null != datetime && !datetime.isEmpty()) {  
  55.             result = datetime.substring(016).replace(" ""-").replace(":""-");  
  56.             if (null != result && !result.isEmpty()) {  
  57.                 String[] arrtime = result.split("-");  
  58.                 if (null != arrtime) {  
  59.                     result = arrtime[1] + "月" + arrtime[2] + "日  " + arrtime[3] + "时" + arrtime[4] + "分";  
  60.                 }  
  61.             }  
  62.         }  
  63.         return result;  
  64.     }  
  65.   
  66.     /** 
  67.      * 获取前n天的日期 2种表现方式 
  68.      * 
  69.      * @return 前n天的日期 2种表现方式 
  70.      */  
  71.     public static Map<String, String> getStatetime(int daynum) throws ParseException {  
  72.   
  73.         Map<String, String> retMap = new HashMap<String, String>();  
  74.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");  
  75.         SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd");  
  76.         Calendar c = Calendar.getInstance();  
  77.         c.add(Calendar.DATE, -daynum);  
  78.         Date monday = c.getTime();  
  79.         String pretime = sdf.format(monday);  
  80.         String preDay = sdf2.format(monday);  
  81.         retMap.put("time", pretime);  
  82.         retMap.put("preDay", preDay);  
  83.         return retMap;  
  84.     }  
  85.   
  86.     /** 
  87.      * 获取两个日期之间的日期 
  88.      * 
  89.      * @param start 开始日期 
  90.      * @param end   结束日期 
  91.      * @return 日期集合 
  92.      */  
  93.     public static List<String> getBetweenDates(String inpstart, String inpend) {  
  94.   
  95.         //时间转换类  
  96.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");  
  97.         Date start = null;  
  98.         Date end = null;  
  99.         try {  
  100.             start = sdf.parse(inpstart);  
  101.             end = sdf.parse(inpend);  
  102.         } catch (java.text.ParseException e) {  
  103.             // TODO Auto-generated catch block  
  104.             e.printStackTrace();  
  105.         }  
  106.         List<String> result = new ArrayList<String>();  
  107.         result.add(inpstart);  
  108.         Calendar tempStart = Calendar.getInstance();  
  109.         tempStart.setTime(start);  
  110.         tempStart.add(Calendar.DAY_OF_YEAR, 1);  
  111.         Calendar tempEnd = Calendar.getInstance();  
  112.         tempEnd.setTime(end);  
  113.         while (tempStart.before(tempEnd)) {  
  114.             result.add(sdf.format(tempStart.getTime()));  
  115.             tempStart.add(Calendar.DAY_OF_YEAR, 1);  
  116.         }  
  117.         result.add(inpend);  
  118.         return result;  
  119.     }  
  120.   
  121.     /** 
  122.      * 获取两个日期之间的日期 
  123.      * 
  124.      * @param start 开始日期 
  125.      * @param end   结束日期 
  126.      * @return 日期集合 
  127.      *时间格式为YYYYMMDD 
  128.      */  
  129.     public static List<String> getBetweenRealDates(String inpstart, String inpend) {  
  130.   
  131.         //时间转换类  
  132.         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
  133.         Date start = null;  
  134.         Date end = null;  
  135.         try {  
  136.             start = sdf.parse(inpstart);  
  137.             end = sdf.parse(inpend);  
  138.         } catch (java.text.ParseException e) {  
  139.             // TODO Auto-generated catch block  
  140.             e.printStackTrace();  
  141.         }  
  142.         List<String> result = new ArrayList<String>();  
  143.         result.add(inpstart);  
  144.         Calendar tempStart = Calendar.getInstance();  
  145.         tempStart.setTime(start);  
  146.         tempStart.add(Calendar.DAY_OF_YEAR, 1);  
  147.         Calendar tempEnd = Calendar.getInstance();  
  148.         tempEnd.setTime(end);  
  149.         while (tempStart.before(tempEnd)) {  
  150.             result.add(sdf.format(tempStart.getTime()));  
  151.             tempStart.add(Calendar.DAY_OF_YEAR, 1);  
  152.         }  
  153.         result.add(inpend);  
  154.         return result;  
  155.     }  
  156.   
  157.     /** 
  158.      * 获取一个日期的月日 
  159.      * 
  160.      * @param start 开始日期 
  161.      * @param end   结束日期 
  162.      * @return 日期 
  163.      */  
  164.     public static String getDateDay(String time) {  
  165.   
  166.         //时间转换类  
  167.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");  
  168.         SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd");  
  169.         Date temptime = null;  
  170.         try {  
  171.             temptime = sdf.parse(time);  
  172.         } catch (java.text.ParseException e) {  
  173.             // TODO Auto-generated catch block  
  174.             e.printStackTrace();  
  175.         }  
  176.         String DateDay = sdf2.format(temptime);  
  177.         return DateDay;  
  178.     }  
  179.   
  180.     /** 
  181.      * 获取一个月的所有日期 
  182.      * 
  183.      * @param start 开始日期 
  184.      * @return 日期 
  185.      */  
  186.     public static String[] getMonthDays(String date) {  
  187.   
  188.         Calendar calendar = Calendar.getInstance();  
  189.         calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(04)));  
  190.         calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(57)) - 1);  
  191.         int month = Integer.parseInt(date.substring(57));  
  192.         int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);  
  193.         String[] days = new String[maxDay];  
  194.         for (int i = 1; i <= maxDay; i++) {  
  195.             days[i - 1] = String.valueOf(month) + "月" + String.valueOf(i) + "日";  
  196.         }  
  197.         return days;  
  198.     }  
  199.   
  200.     /** 
  201.      * 获取输入日期的前7天 
  202.      * 
  203.      * @param date 开始日期 
  204.      */  
  205.     public static String[] getSevenDays(String date) {  
  206.   
  207.         Calendar calendar = Calendar.getInstance();  
  208.         calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(04)));  
  209.         calendar.set(Calendar.DATE, Integer.parseInt(date.substring(57)) - 1);  
  210.         int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_WEEK);//maxDay-----7天  
  211.         String[] days = new String[maxDay];  
  212.         for (int i = 1; i <= maxDay; i++) {  
  213.             days[i - 1] = DateUtil.getBeforeDay(date, i - 8);  
  214.   
  215.         }  
  216.         return days;  
  217.     }  
  218.   
  219.     /** 
  220.      * 获取当前日期是星期几 
  221.      * @return 当前日期是星期几 
  222.      */  
  223.     public static int getWeekNum(String time) {  
  224.   
  225.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");  
  226.         Date temptime = null;  
  227.         try {  
  228.             temptime = sdf.parse(time);  
  229.         } catch (java.text.ParseException e) {  
  230.             // TODO Auto-generated catch block  
  231.             e.printStackTrace();  
  232.         }  
  233.         Calendar cal = Calendar.getInstance();  
  234.         cal.setTime(temptime);  
  235.         int w = cal.get(Calendar.DAY_OF_WEEK) - 1;  
  236.         if (w < 0)  
  237.             w = 0;  
  238.         return w;  
  239.     }  
  240.   
  241.     /** 
  242.      * 获取当前日期格式,由YYYY/MM/DD--》YYYYMMDD 
  243.      * @return 当前日期是星期几 
  244.      */  
  245.     public static String UpdateFormat(String time) {  
  246.   
  247.         String newtime = "";  
  248.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");  
  249.         SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");  
  250.         if (null != time && !time.isEmpty()) {  
  251.             try {  
  252.                 Date temptime = null;  
  253.                 try {  
  254.                     temptime = (Date) sdf.parse(time);  
  255.                 } catch (java.text.ParseException e) {  
  256.                     e.printStackTrace();  
  257.                 }  
  258.                 newtime = sdf2.format(temptime);  
  259.             } catch (ParseException e) {  
  260.                 e.printStackTrace();  
  261.             }  
  262.         }  
  263.         return newtime;  
  264.     }  
  265.   
  266.     /** 
  267.      * 获取当前日期格式,由YYYYMMDD--》YYYY/MM/DD 
  268.      * @return 当前日期是星期几 
  269.      */  
  270.     public static String UpdateDateFormat(String time) {  
  271.   
  272.         String newtime = "";  
  273.         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
  274.         SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");  
  275.         if (null != time && !time.isEmpty()) {  
  276.             try {  
  277.                 Date temptime = null;  
  278.                 try {  
  279.                     temptime = (Date) sdf.parse(time);  
  280.                 } catch (java.text.ParseException e) {  
  281.                     e.printStackTrace();  
  282.                 }  
  283.                 newtime = sdf2.format(temptime);  
  284.             } catch (ParseException e) {  
  285.                 e.printStackTrace();  
  286.             }  
  287.         }  
  288.         return newtime;  
  289.     }  
  290.   
  291.     /** 
  292.      * 获取当前日期格式,由YYYY/MM--》YYYYMM 
  293.      * @return 当前日期是星期几 
  294.      */  
  295.     public static String UpdateFormatMonth(String time) {  
  296.   
  297.         String newtime = "";  
  298.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");  
  299.         SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMM");  
  300.         try {  
  301.             Date temptime = null;  
  302.             try {  
  303.                 temptime = (Date) sdf.parse(time);  
  304.             } catch (java.text.ParseException e) {  
  305.                 // TODO Auto-generated catch block  
  306.                 e.printStackTrace();  
  307.             }  
  308.             newtime = sdf2.format(temptime);  
  309.         } catch (ParseException e) {  
  310.             // TODO Auto-generated catch block  
  311.             e.printStackTrace();  
  312.         }  
  313.         return newtime;  
  314.     }  
  315.   
  316.     /** 
  317.      * 获取当前日期格式,由YYYY-MM-DD--》YYYYMMDD 
  318.      * @return 当前日期是星期几 
  319.      */  
  320.     public static String UpdateFormatReal(String time) {  
  321.   
  322.         String newtime = "";  
  323.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  324.         SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");  
  325.         if (null != time && !time.isEmpty()) {  
  326.             try {  
  327.                 Date temptime = null;  
  328.                 try {  
  329.                     temptime = (Date) sdf.parse(time);  
  330.                 } catch (java.text.ParseException e) {  
  331.                     e.printStackTrace();  
  332.                 }  
  333.                 newtime = sdf2.format(temptime);  
  334.             } catch (ParseException e) {  
  335.                 e.printStackTrace();  
  336.             }  
  337.         }  
  338.         return newtime;  
  339.     }  

猜你喜欢

转载自blog.csdn.net/u010682330/article/details/80741865
今日推荐