Java 日期类算法 转(转载)

对日期的操作主要可以用 
java.text.SimpleDateFormat 
java.util.Calendar 
java.util.Date 
进行操作,也可以用apacle commons中的commons-lang包下的 
org.apache.commons.lang.time.DateUtils来对日期就行操作 
下面写的一些操作日期的的工具类 

Java代码   收藏代码
  1. import java.text.DateFormat;     
  2. import java.text.ParseException;     
  3. import java.text.SimpleDateFormat;     
  4. import java.util.ArrayList;     
  5. import java.util.Calendar;     
  6. import java.util.Date;     
  7. import java.util.LinkedHashSet;     
  8. import java.util.List;     
  9. import java.util.Locale;     
  10. .import java.util.Set;     
  11. import org.apache.log4j.Logger;     
  12.    
  13. public class DateUtils {     
  14.    private static final Logger log = Logger.getLogger(DateUtils.class);     
  15.    
  16.    /**   
  17.     * 将字符串日期转换为Date   
  18.     *    
  19.     * @param s   
  20.     * @return   
  21.     */    
  22.    public static Date convertToDate(String s) {     
  23.        DateFormat df;     
  24.        if (s == null) {     
  25.            return null;     
  26.        }     
  27.        if (s.contains(":")) {     
  28.            try {     
  29.                df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     
  30.                return df.parse(s);     
  31.            } catch (Exception e) {     
  32.                log.error(e);     
  33.                return null;     
  34.            }     
  35.        } else {     
  36.            try {     
  37.                df = new SimpleDateFormat("yyyy-MM-dd");     
  38.                return df.parse(s);     
  39.            } catch (Exception e) {     
  40.                log.error(e);     
  41.                return null;     
  42.            }     
  43.        }     
  44.    }     
  45.    
  46.    /**   
  47.     * 将Date转换为String   
  48.     *    
  49.     * @param d   
  50.     * @return   
  51.     */    
  52.    public static String formatDate(Date d) {     
  53.        if (d == null) {     
  54.            return null;     
  55.        }     
  56.        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     
  57.        try {     
  58.            return sdf.format(d);     
  59.        } catch (Exception e) {     
  60.            log.error(e);     
  61.            return null;     
  62.        }     
  63.    }     
  64.    
  65.    /**   
  66.     * 将Date转换为String   
  67.     *    
  68.     * @param d   
  69.     * @return   
  70.     */    
  71.    public static String formatTime(Date d) {     
  72.        if (d == null) {     
  73.            return null;     
  74.        }     
  75.        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");     
  76.        try {     
  77.            return sdf.format(d);     
  78.        } catch (Exception e) {     
  79.            log.error(e);     
  80.            return null;     
  81.        }     
  82.    }     
  83.    
  84.    public static String formatTimeHHmm(Date d) {     
  85.        if (d == null) {     
  86.            return null;     
  87.        }     
  88.        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");     
  89.        try {     
  90.            return sdf.format(d);     
  91.        } catch (Exception e) {     
  92.            log.error(e);     
  93.            return null;     
  94.        }     
  95.    }     
  96.    
  97.    /**   
  98.     * 将Date按locale转换为String   
  99.     *    
  100.      * @param d   
  101.      * @return   
  102.      */    
  103.     public static String formatLocaleDate(Date d, Locale locale) {     
  104.         if (d == null) {     
  105.             return null;     
  106.         }     
  107.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", locale);     
  108.         try {     
  109.             return sdf.format(d);     
  110.         } catch (Exception e) {     
  111.             log.error(e);     
  112.             return null;     
  113.         }     
  114.     }     
  115.     
  116.     /**   
  117.      * 将Date按locale转换为String   
  118.      *    
  119.      * @param d   
  120.      * @return   
  121.      */    
  122.     public static String formatLocaleDateTime(Date d, Locale locale) {     
  123.         if (d == null) {     
  124.             return null;     
  125.         }     
  126.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale);     
  127.         try {     
  128.             return sdf.format(d);     
  129.         } catch (Exception e) {     
  130.             log.error(e);     
  131.             return null;     
  132.         }     
  133.     }     
  134.     
  135.     /**   
  136.      * 将Date转换为String   
  137.      *    
  138.      * @param d   
  139.      * @return   
  140.      */    
  141.     public static String formatDateTime(Date d) {     
  142.         if (d == null) {     
  143.             return null;     
  144.         }     
  145.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     
  146.         try {     
  147.             return sdf.format(d);     
  148.         } catch (Exception e) {     
  149.             log.error(e);     
  150.             return null;     
  151.         }     
  152.     }     
  153.     
  154.     /**   
  155.      * 得到每月多少天   
  156.      *    
  157.      * @param year   
  158.      * @param month   
  159.      * @return 返回 -1表示异常   
  160.      */    
  161.     public static int getDaysByMonth(int year, int month) {     
  162.         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {     
  163.             return 31;     
  164.         }     
  165.     
  166.         if (month == 4 || month == 6 || month == 9 || month == 11)     
  167.             return 30;     
  168.     
  169.         if (month == 2) {     
  170.             if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {     
  171.                 return 29;     
  172.             } else {     
  173.                 return 28;     
  174.             }     
  175.         }     
  176.         return -1;     
  177.     }     
  178.     
  179.     public static String dayOfWeekByDayNum(int x) {     
  180.         String str = "周日";     
  181.         if (x == 7) {     
  182.             str = "周六";     
  183.         } else if (x == 6) {     
  184.             str = "周五";     
  185.         } else if (x == 5) {     
  186.             str = "周四";     
  187.         } else if (x == 4) {     
  188.             str = "周三";     
  189.         } else if (x == 3) {     
  190.             str = "周二";     
  191.         } else if (x == 2) {     
  192.             str = "周一";     
  193.         }     
  194.         return str;     
  195.     }     
  196.     
  197.     /**   
  198.      * 据年、月、日,获得当天为周几   
  199.      *    
  200.      * @param year   
  201.      * @param month   
  202.      * @param day   
  203.      * @return   
  204.      */    
  205.     public static int getWeekByDate(int year, int month, int day) {     
  206.         Calendar c = Calendar.getInstance();     
  207.         c.set(Calendar.YEAR, year);     
  208.         c.set(Calendar.MONTH, month - 1);     
  209.         c.set(Calendar.DAY_OF_MONTH, day);     
  210.     
  211.         return c.get(Calendar.DAY_OF_WEEK);     
  212.     }     
  213.     
  214.     /**   
  215.      * 得到现在是这个周的第几天   
  216.      *    
  217.      * @return   
  218.      */    
  219.     public static int getWeekByDate() {     
  220.         Calendar c = Calendar.getInstance();     
  221.         return c.get(Calendar.DAY_OF_WEEK);     
  222.     }     
  223.     
  224.     public static List<String> monthDiff(Date date1, Date date2) throws Exception {     
  225.         List<String> monthList = new ArrayList<String>();     
  226.     
  227.         if (DateUtils.dateDiff(date1, date2) < 0) {     
  228.             return monthList;     
  229.         }     
  230.     
  231.         Calendar calendar1 = Calendar.getInstance();     
  232.         Calendar calendar2 = Calendar.getInstance();     
  233.         calendar1.setTime(date1);     
  234.         calendar2.setTime(date2);     
  235.         while (DateUtils.dateDiff(calendar1.getTime(), calendar2.getTime()) >= 0) {     
  236.             monthList.add(DateUtils.formatDate(calendar1.getTime()));     
  237.             calendar1.set(Calendar.DAY_OF_MONTH, 1);     
  238.             calendar1.set(Calendar.MONTH, calendar1.get(Calendar.MONTH) + 1);     
  239.         }     
  240.         if (monthList.size() > 0) {     
  241.             monthList.remove(monthList.size() - 1);     
  242.             monthList.add(DateUtils.formatDate(date2));     
  243.         }     
  244.     
  245.         return monthList;     
  246.     }     
  247.     
  248.     /**   
  249.      * 计算两个日期之间相差多少天   
  250.      *    
  251.      * @param date1   
  252.      * @param date2   
  253.      * @return   
  254.      */    
  255.     public static int dateDiff(Date date1, Date date2) {     
  256.         Calendar calendar1 = Calendar.getInstance();     
  257.         Calendar calendar2 = Calendar.getInstance();     
  258.         calendar1.setTime(date1);     
  259.         calendar2.setTime(date2);     
  260.         long increaseDate = (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / 1000 / 60 / 60 / 24;     
  261.         if (((calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) % (1000 * 60 * 60 * 24)) > 0) {     
  262.             increaseDate = increaseDate + 1;     
  263.         }     
  264.         return (int) increaseDate;     
  265.     }     
  266.     
  267.     /**   
  268.      * 取得两天之间的天数   
  269.      *    
  270.      * @param start   
  271.      * @param end   
  272.      * @return   
  273.      */    
  274.     public static int daysBetween(Date start, Date end) {     
  275.         // return date1.getTime() / (24*60*60*1000) - date2.getTime() /     
  276.         // (24*60*60*1000);     
  277.         String formatDate = formatDate(start);     
  278.         Date convertStartDate = convertToDate(formatDate);     
  279.         formatDate = formatDate(end);     
  280.         Date convertEndDate = convertToDate(formatDate);     
  281.         return (int) (convertEndDate.getTime() / 86400000 - convertStartDate.getTime() / 86400000); // 用立即数,减少乘法计算的开销     
  282.     }     
  283.     
  284.     /**   
  285.      * 取得两天之间的日期数组,包含开始日期与结束日期   
  286.      *    
  287.      * @param startDateStr   
  288.      *            开始日期   
  289.      * @param endDateStr   
  290.      *            结束日期   
  291.      * @return Date[] 日期数组   
  292.      */    
  293.     public static Date[] getBetweenTwoDayArray(String startDateStr, String endDateStr) {     
  294.         Date startDate = formatDateYyyyMmDd(startDateStr);     
  295.         int dateNum = Integer.parseInt(getDaysBetweenTwoDates(startDateStr, endDateStr)) + 1;     
  296.         Date[] dataArray = new Date[dateNum];     
  297.         for (int i = 0; i < dateNum; i++) {     
  298.             dataArray[i] = startDate;     
  299.             startDate = org.apache.commons.lang.time.DateUtils.addDays(startDate, 1);     
  300.         }     
  301.         return dataArray;     
  302.     }     
  303.     
  304.     /**   
  305.      * 把日期字符串格式化为日期类型   
  306.      *    
  307.      * @param datetext   
  308.      * @return   
  309.      */    
  310.     public static Date formatDateYyyyMmDd(String datetext) {     
  311.         try {     
  312.             SimpleDateFormat df;     
  313.             if (datetext == null || "".equals(datetext.trim())) {     
  314.                 return null;     
  315.             }     
  316.             datetext = datetext.replaceAll("/""-");     
  317.             df = new SimpleDateFormat("yyyy-MM-dd");     
  318.             Date date = df.parse(datetext);     
  319.             return date;     
  320.         } catch (Exception e) {     
  321.             e.printStackTrace();     
  322.             return null;     
  323.         }     
  324.     }     
  325.     
  326.     /*   
  327.      * 两个日期之间相隔天数的共通 author:jerry.ji date:08-03-06   
  328.      *    
  329.      * @param from 開始時間   
  330.      *    
  331.      * @param to 終了時間   
  332.      *    
  333.      * @return 天数   
  334.      */    
  335.     public static String getDaysBetweenTwoDates(String dateFrom, String dateEnd) {     
  336.         Date dtFrom = null;     
  337.         Date dtEnd = null;     
  338.         dtFrom = string2Date(dateFrom, "yyyy-MM-dd");     
  339.         dtEnd = string2Date(dateEnd, "yyyy-MM-dd");     
  340.         long begin = dtFrom.getTime();     
  341.         long end = dtEnd.getTime();     
  342.         long inter = end - begin;     
  343.         if (inter < 0) {     
  344.             inter = inter * (-1);     
  345.         }     
  346.         long dateMillSec = 24 * 60 * 60 * 1000;     
  347.     
  348.         long dateCnt = inter / dateMillSec;     
  349.     
  350.         long remainder = inter % dateMillSec;     
  351.     
  352.         if (remainder != 0) {     
  353.             dateCnt++;     
  354.         }     
  355.         return String.valueOf(dateCnt);     
  356.     }     
  357.     
  358.     /**   
  359.      * 把日期字符串格式化为日期类型   
  360.      *    
  361.      * @param datetext   
  362.      *            日期字符串   
  363.      * @param format   
  364.      *            日期格式,如果不传则使用"yyyy-MM-dd HH:mm:ss"格式   
  365.      * @return   
  366.      */    
  367.     public static Date string2Date(String datetext, String format) {     
  368.         try {     
  369.             SimpleDateFormat df;     
  370.             if (datetext == null || "".equals(datetext.trim())) {     
  371.                 return new Date();     
  372.             }     
  373.             if (format != null) {     
  374.                 df = new SimpleDateFormat(format);     
  375.             } else {     
  376.                 datetext = datetext.replaceAll("/""-");     
  377.                 df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     
  378.             }     
  379.     
  380.             Date date = df.parse(datetext);     
  381.     
  382.             return date;     
  383.     
  384.         }     
  385.     
  386.         catch (Exception e) {     
  387.             e.printStackTrace();     
  388.             return new Date();     
  389.         }     
  390.     }     
  391.     
  392.     public static String formatDate(Date date, String format) {     
  393.         try {     
  394.             if (format != null && !"".equals(format) && date != null) {     
  395.                 SimpleDateFormat formatter = new SimpleDateFormat(format);     
  396.                 return formatter.format(date);     
  397.             }     
  398.         } catch (Exception e) {     
  399.             return "";     
  400.         }     
  401.         return "";     
  402.     }     
  403.     
  404.     /**   
  405.      * 算出两个日期中所包含的月份   
  406.      *    
  407.      * @param fromDate   
  408.      * @param toDate   
  409.      * @return   
  410.      */    
  411.     public static Set<String> getMonthBetweenTwoDate(Date fromDate, Date toDate) {     
  412.         long begin = fromDate.getTime();     
  413.         long end = toDate.getTime();     
  414.         long inter = end - begin;     
  415.         if (inter < 0) {     
  416.             inter = inter * (-1);     
  417.         }     
  418.         long dateMillSec = 86400000;     
  419.         long dateCnt = inter / dateMillSec;     
  420.         long remainder = inter % dateMillSec;     
  421.         if (remainder != 0) {     
  422.             dateCnt++;     
  423.         }     
  424.         Set<String> set = new LinkedHashSet<String>();     
  425.         Calendar cl = Calendar.getInstance();     
  426.         cl.setTime(fromDate);     
  427.         cl.set(Calendar.HOUR_OF_DAY, 0);     
  428.         cl.set(Calendar.MINUTE, 0);     
  429.         cl.set(Calendar.SECOND, 0);     
  430.         cl.set(Calendar.MILLISECOND, 0);     
  431.         set.add(getDateyyyyMM(cl.getTime()));     
  432.         for (int i = 1; i <= dateCnt; i++) {     
  433.             cl.add(Calendar.DAY_OF_YEAR, 1);     
  434.             set.add(getDateyyyyMM(cl.getTime()));     
  435.         }     
  436.         return set;     
  437.     }     
  438.     
  439.     /**   
  440.      * 得到yyyyMM的年月   
  441.      *    
  442.      * @param date   
  443.      * @return   
  444.      */    
  445.     public static String getDateyyyyMM(Date date) {     
  446.         SimpleDateFormat df = new SimpleDateFormat("yyyyMM");     
  447.         return df.format(date);     
  448.     }     
  449.     
  450.     /**   
  451.      * 得到yyyyMM的年月   
  452.      *    
  453.      * @param date   
  454.      * @return   
  455.      */    
  456.     public static String getDateyyyyMMdd(Date date) {     
  457.         SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");     
  458.         return df.format(date);     
  459.     }     
  460.     
  461.     /**   
  462.      * 得到一月的最大天数   
  463.      *    
  464.      * @param date   
  465.      * @return   
  466.      */    
  467.     public static int getMonthsMaxDay(Date date) {     
  468.         Calendar cal1 = Calendar.getInstance();     
  469.         cal1.setTime(date);     
  470.         return cal1.getActualMaximum(cal1.DAY_OF_MONTH);     
  471.     }     
  472.     
  473.     public static Date parseDateyyyyMM(String month) {     
  474.         SimpleDateFormat df = new SimpleDateFormat("yyyyMM");     
  475.         try {     
  476.             return df.parse(month);     
  477.         } catch (ParseException e) {     
  478.         }     
  479.         return null;     
  480.     }     
  481.     
  482.     public static Date parseDateyyyyMMdd(String date) {     
  483.         SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");     
  484.         try {     
  485.             return df.parse(date);     
  486.         } catch (ParseException e) {     
  487.         }     
  488.         return null;     
  489.     }     
  490.     
  491.     /**   
  492.      * 根据两个日期,算出某个月份的第一天,或者最后一天   
  493.      *    
  494.      * @param dateFrom   
  495.      * @param dateTo   
  496.      * @param month   
  497.      * @param flag   
  498.      * @return   
  499.      */    
  500.     public static int getDayBetweenDateStartOrEnd(Date dateFrom, Date dateTo, Date month, String flag) {     
  501.         if (dateFrom.getTime() > dateTo.getTime()) {     
  502.             Date temp = dateFrom;     
  503.             dateFrom = dateTo;     
  504.             dateTo = temp;     
  505.         }     
  506.         if (getDateyyyyMM(month).compareTo(getDateyyyyMM(dateFrom)) > 0    
  507.                 && getDateyyyyMM(month).compareTo(getDateyyyyMM(dateTo)) < 0) {     
  508.             if ("start".equals(flag))     
  509.                 return 1;     
  510.             return getMonthsMaxDay(month);     
  511.         } else if (getDateyyyyMM(month).compareTo(getDateyyyyMM(dateFrom)) == 0    
  512.                 && getDateyyyyMM(month).compareTo(getDateyyyyMM(dateTo)) < 0) {     
  513.             if ("start".equals(flag))     
  514.                 return getDayOfMonth(dateFrom);     
  515.             return getMonthsMaxDay(month);     
  516.         } else if (getDateyyyyMM(month).compareTo(getDateyyyyMM(dateFrom)) > 0    
  517.                 && getDateyyyyMM(month).compareTo(getDateyyyyMM(dateTo)) == 0) {     
  518.             if ("start".equals(flag))     
  519.                 return 1;     
  520.             return getDayOfMonth(dateTo);     
  521.         } else {     
  522.             if ("start".equals(flag))     
  523.                 return getDayOfMonth(dateFrom);     
  524.             return getDayOfMonth(dateTo);     
  525.         }     
  526.     
  527.     }     
  528.     
  529.     /**   
  530.      * 根据一个日期,算出是这个月中的第几天   
  531.      *    
  532.      * @param date   
  533.      * @return   
  534.      */    
  535.     public static int getDayOfMonth(Date date) {     
  536.         Calendar cal1 = Calendar.getInstance();     
  537.         cal1.setTime(date);     
  538.         return cal1.get(Calendar.DAY_OF_MONTH);     
  539.     }     
  540.     
  541.     /**   
  542.      * 取出一个月中某天的日期   
  543.      *    
  544.      * @param month   
  545.      * @param num   
  546.      * @return   
  547.      */    
  548.     public static Date getDateOfMonthByNum(String month, int num) {     
  549.         Calendar cl = Calendar.getInstance();     
  550.         cl.setTime(parseDateyyyyMM(month));     
  551.         cl.set(Calendar.HOUR_OF_DAY, 0);     
  552.         cl.set(Calendar.MINUTE, 0);     
  553.         cl.set(Calendar.SECOND, 0);     
  554.         cl.set(Calendar.MILLISECOND, 0);     
  555.         cl.add(Calendar.DAY_OF_YEAR, num - 1);     
  556.         return cl.getTime();     
  557.     }     
  558.     
  559.     /**   
  560.      * 得到本周第一天日期   
  561.      *    
  562.      * @author vincent.shan   
  563.      * @return   
  564.      */    
  565.     public static Date getCurrentWeekMonday() {     
  566.     
  567.         Calendar cal = Calendar.getInstance();     
  568.         cal.set(Calendar.HOUR_OF_DAY, 0);     
  569.         cal.set(Calendar.MINUTE, 0);     
  570.         cal.set(Calendar.SECOND, 0);     
  571.         cal.set(Calendar.MILLISECOND, 0);     
  572.         int index = cal.get(Calendar.DAY_OF_WEEK);     
  573.         // 转成中国的习惯,如果是第一天,则转化为第七天(星期天外国为一周的第一天,而中国为每周的最后一天)     
  574.         if (index == 1)     
  575.             index = 8;     
  576.         cal.add(Calendar.DATE, -(index - 2));     
  577.         return cal.getTime();     
  578.     
  579.     }     
  580.     
  581.     /**   
  582.      * 得到本周最后一天的日期   
  583.      *    
  584.      * @author vincent.shan   
  585.      * @return   
  586.      */    
  587.     public static Date getCurrentWeekSaturday() {     
  588.         Calendar cal = Calendar.getInstance();     
  589.         cal.set(Calendar.HOUR_OF_DAY, 0);     
  590.         cal.set(Calendar.MINUTE, 0);     
  591.         cal.set(Calendar.SECOND, 0);     
  592.         cal.set(Calendar.MILLISECOND, 0);     
  593.         int index = cal.get(Calendar.DAY_OF_WEEK);     
  594.         if (index == 1)     
  595.             index = 8;     
  596.         cal.add(Calendar.DATE, -(index - 2));     
  597.         cal.add(Calendar.DATE, 6);     
  598.         return cal.getTime();     
  599.     }     
  600.     
  601.     /**   
  602.      * 从指定日期移动一定的天数   
  603.      *    
  604.      * @param date   
  605.      * @param day   
  606.      * @return   
  607.      */    
  608.     public static Date moveDay(Date date, int day) {     
  609.         Calendar cal = Calendar.getInstance();     
  610.         cal.setTime(date);     
  611.         cal.set(Calendar.HOUR_OF_DAY, 0);     
  612.         cal.set(Calendar.MINUTE, 0);     
  613.         cal.set(Calendar.SECOND, 0);     
  614.         cal.set(Calendar.MILLISECOND, 0);     
  615.         cal.add(Calendar.DAY_OF_MONTH, day);     
  616.         return cal.getTime();     
  617.     }     
  618.     
  619.     /**   
  620.      * 从当天日期移动一定的月数   
  621.      *    
  622.      * @param month   
  623.      * @return   
  624.      */    
  625.     
  626.     public static Date getMoveMonthDate(int month) {     
  627.         Date nowDate = new Date();     
  628.         Calendar cl = Calendar.getInstance();     
  629.         cl.setTime(nowDate);     
  630.         cl.add(Calendar.MONDAY, month - 1);     
  631.         Date date1 = cl.getTime();     
  632.         return date1;     
  633.     }     
  634.     
  635.     /**   
  636.      * 得到昨天   
  637.      *    
  638.      * @param date   
  639.      * @param day   
  640.      * @return   
  641.      */    
  642.     public static Date getYesterday() {     
  643.         Calendar cal = Calendar.getInstance();     
  644.     
  645.         cal.set(Calendar.HOUR_OF_DAY, 0);     
  646.         cal.set(Calendar.MINUTE, 0);     
  647.         cal.set(Calendar.SECOND, 0);     
  648.         cal.set(Calendar.MILLISECOND, 0);     
  649.         cal.add(Calendar.DAY_OF_MONTH, -1);     
  650.         return cal.getTime();     
  651.     }     
  652.     
  653.     /**   
  654.      * 根据某个日期,返回本月第一天   
  655.      *    
  656.      * @param date   
  657.      *            任何一天   
  658.      * @return Date 当月第一天   
  659.      * */    
  660.     public static Date getMonthsFirstDay(Date date) {     
  661.         Calendar cal = Calendar.getInstance();     
  662.         cal.setTime(date);     
  663.         cal.set(Calendar.DATE, 1);     
  664.         cal.set(Calendar.HOUR_OF_DAY, 0);     
  665.         cal.set(Calendar.MINUTE, 0);     
  666.         cal.set(Calendar.SECOND, 0);     
  667.         cal.set(Calendar.MILLISECOND, 0);     
  668.         return cal.getTime();     
  669.     }     
  670.     
  671.     /**   
  672.      * 根据某个日期,返回本月最后一天   
  673.      *    
  674.      * @param date   
  675.      *            任何一天   
  676.      * @return Date 当月第一天   
  677.      * */    
  678.     public static Date getMonthsLastDay(Date date) {     
  679.         Calendar cal = Calendar.getInstance();     
  680.         cal.setTime(date);     
  681.         cal.set(Calendar.DATE, 1);     
  682.         cal.set(Calendar.HOUR_OF_DAY, 0);     
  683.         cal.set(Calendar.MINUTE, 0);     
  684.         cal.set(Calendar.SECOND, 0);     
  685.         cal.set(Calendar.MILLISECOND, 0);     
  686.         cal.add(Calendar.MONTH, 1);     
  687.         cal.add(Calendar.DATE, -1);     
  688.         return cal.getTime();     
  689.     }     
  690.     
  691.     public static Set<String> getDayList(Date startDate, Date endDate) {     
  692.         long begin = startDate.getTime();     
  693.         long end = endDate.getTime();     
  694.         long inter = end - begin;     
  695.         if (inter < 0) {     
  696.             inter = inter * (-1);     
  697.         }     
  698.         long dateMillSec = 86400000;     
  699.         long dateCnt = inter / dateMillSec;     
  700.         Set<String> set = new LinkedHashSet<String>();     
  701.         Calendar cl = Calendar.getInstance();     
  702.         cl.setTime(startDate);     
  703.         cl.set(Calendar.HOUR_OF_DAY, 0);     
  704.         cl.set(Calendar.MINUTE, 0);     
  705.         cl.set(Calendar.SECOND, 0);     
  706.         cl.set(Calendar.MILLISECOND, 0);     
  707.         set.add(getDateyyyyMMdd(cl.getTime()));     
  708.         for (int i = 1; i <= dateCnt; i++) {     
  709.             cl.add(Calendar.DAY_OF_YEAR, 1);     
  710.             set.add(getDateyyyyMMdd(cl.getTime()));     
  711.         }     
  712.         set.add(getDateyyyyMMdd(endDate));     
  713.         return set;     
  714.     }     
  715.     
  716.     /**   
  717.      * 功能:取得两个日期中最小的日期,如果两个日期参数都为null则返回null   
  718.      *    
  719.      * @author irvshan   
  720.      *    
  721.      * @param date1   
  722.      * @param date2   
  723.      * @return Date or null   
  724.      */    
  725.     public static Date getMinimizeDate(Date date1, Date date2) {     
  726.         if (date1 == null) {     
  727.             return date2;     
  728.         }     
  729.         if (date2 == null) {     
  730.             return date1;     
  731.         }     
  732.         if (date1.compareTo(date2) > 0) {     
  733.             return date2;     
  734.         }     
  735.         return date1;     
  736.     }     
  737.     
  738.     /**   
  739.      * 功能:取得两个日期中最大的日期,如果两个日期参数都为null则返回null   
  740.      *    
  741.      * @author irvshan   
  742.      *    
  743.      * @param date1   
  744.      * @param date2   
  745.      * @return Date or null   
  746.      */    
  747.     public static Date getMaxmizeDate(Date date1, Date date2) {     
  748.         if (date1 == null) {     
  749.             return date2;     
  750.         }     
  751.         if (date2 == null) {     
  752.             return date1;     
  753.         }     
  754.         if (date1.compareTo(date2) < 0) {     
  755.             return date2;     
  756.         }     
  757.         return date1;     
  758.     }     
  759.     
  760.     public static int getDayofWeek(Date date, int day) {     
  761.         Calendar calendar = Calendar.getInstance();     
  762.         calendar.setTime(date);     
  763.         calendar.set(Calendar.HOUR_OF_DAY, 0);     
  764.         calendar.set(Calendar.MINUTE, 0);     
  765.         calendar.set(Calendar.SECOND, 0);     
  766.         calendar.set(Calendar.MILLISECOND, 0);     
  767.         calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + day - 1);     
  768.         return calendar.get(Calendar.DAY_OF_WEEK);     
  769.     }     
  770.     
  771.     /**   
  772.      * 把date1的小时,分钟,秒换成date2的小时,分钟,秒,返回换值后的date1   
  773.      *    
  774.      * @param date1   
  775.      * @param date2   
  776.      * @return   
  777.      */    
  778.     public static Date changeTheHourMinuteSecond(Date date1, Date date2) {     
  779.         Calendar cl1 = Calendar.getInstance();     
  780.         cl1.setTime(date1);     
  781.         cl1.set(Calendar.HOUR_OF_DAY, 0);     
  782.         cl1.set(Calendar.MINUTE, 0);     
  783.         cl1.set(Calendar.SECOND, 0);     
  784.     
  785.         Calendar cl2 = Calendar.getInstance();     
  786.         cl2.setTime(date2);     
  787.         cl1.set(Calendar.HOUR_OF_DAY, cl2.get(Calendar.HOUR_OF_DAY));     
  788.         cl1.set(Calendar.MINUTE, cl2.get(Calendar.MINUTE));     
  789.         cl1.set(Calendar.SECOND, cl2.get(Calendar.SECOND));     
  790.     
  791.         return cl1.getTime();     
  792.     }     
  793.     
  794.     public static void main(String[] args) {     
  795.     
  796.     }     
  797.     
  798. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值