日期工具类,计算周月季度相关值

来源:互联网 发布:java file类能干嘛 编辑:程序博客网 时间:2024/05/21 10:45
Java代码  收藏代码
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Calendar;  
  3. import java.util.Date;  
  4.   
  5. import org.apache.commons.logging.Log;  
  6. import org.apache.commons.logging.LogFactory;  
  7.   
  8. /** 
  9.  * 
  10.  * @author px 
  11.  * @date 2010-04-26 
  12.  * 
  13.  */  
  14. public class DateUtils {  
  15.   
  16.     private static final Log logger = LogFactory.getLog(DateUtils.class);  
  17.   
  18.     public static final String YYYYMMDD = "yyyy-MM-dd";  
  19.   
  20.     public static final String YYYYMMDD_ZH = "yyyy年MM月dd日";  
  21.   
  22.     public static final int FIRST_DAY_OF_WEEK = Calendar.MONDAY; //中国周一是一周的第一天  
  23.   
  24.     /** 
  25.      * 
  26.      * @param strDate 
  27.      * @return 
  28.      */  
  29.     public static Date parseDate(String strDate) {  
  30.         return parseDate(strDate, null);  
  31.     }  
  32.   
  33.     /** 
  34.      * parseDate 
  35.      * 
  36.      * @param strDate 
  37.      * @param pattern 
  38.      * @return 
  39.      */  
  40.     public static Date parseDate(String strDate, String pattern) {  
  41.         Date date = null;  
  42.         try {  
  43.             if(pattern == null) {  
  44.                 pattern = YYYYMMDD;  
  45.             }  
  46.             SimpleDateFormat format = new SimpleDateFormat(pattern);  
  47.             date = format.parse(strDate);  
  48.         } catch (Exception e) {  
  49.             logger.error("parseDate error:" + e);  
  50.         }  
  51.         return date;  
  52.     }  
  53.   
  54.     /** 
  55.      * format date 
  56.      * 
  57.      * @param date 
  58.      * @return 
  59.      */  
  60.     public static String formatDate(Date date) {  
  61.         return formatDate(date, null);  
  62.     }  
  63.   
  64.     /** 
  65.      * format date 
  66.      * 
  67.      * @param date 
  68.      * @param pattern 
  69.      * @return 
  70.      */  
  71.     public static String formatDate(Date date, String pattern) {  
  72.         String strDate = null;  
  73.         try {  
  74.             if(pattern == null) {  
  75.                 pattern = YYYYMMDD;  
  76.             }  
  77.             SimpleDateFormat format = new SimpleDateFormat(pattern);  
  78.             strDate = format.format(date);  
  79.         } catch (Exception e) {  
  80.             logger.error("formatDate error:", e);  
  81.         }  
  82.         return strDate;  
  83.     }  
  84.   
  85.     /** 
  86.      * 取得一年的第几周 
  87.      * 
  88.      * @param date 
  89.      * @return 
  90.      */  
  91.     public static int getWeekOfYear(Date date){  
  92.         Calendar c = Calendar.getInstance();  
  93.         c.setTime(date);  
  94.         int week_of_year = c.get(Calendar.WEEK_OF_YEAR);  
  95.         return week_of_year - 1;  
  96.     }  
  97.   
  98.     /** 
  99.      * getWeekBeginAndEndDate 
  100.      * 
  101.      * @param date 
  102.      * @param pattern 
  103.      * @return 
  104.      */  
  105.     public static String getWeekBeginAndEndDate(Date date, String pattern){  
  106.         Date monday = getMondayOfWeek(date);  
  107.         Date sunday = getSundayOfWeek(date);  
  108.         return formatDate(monday, pattern) + " - " + formatDate(sunday, pattern) ;  
  109.     }  
  110.   
  111.     /** 
  112.      * 根据日期取得对应周周一日期 
  113.      * 
  114.      * @param date 
  115.      * @return 
  116.      */  
  117.     public static Date getMondayOfWeek(Date date) {  
  118.         Calendar monday = Calendar.getInstance();  
  119.         monday.setTime(date);  
  120.         monday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);  
  121.         monday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
  122.         return monday.getTime();  
  123.     }  
  124.   
  125.     /** 
  126.      * 根据日期取得对应周周日日期 
  127.      * 
  128.      * @param date 
  129.      * @return 
  130.      */  
  131.     public static Date getSundayOfWeek(Date date) {  
  132.         Calendar sunday = Calendar.getInstance();  
  133.         sunday.setTime(date);  
  134.         sunday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);  
  135.         sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);  
  136.         return sunday.getTime();  
  137.     }  
  138.   
  139.     /** 
  140.      * 取得月的剩余天数 
  141.      * 
  142.      * @param date 
  143.      * @return 
  144.      */  
  145.     public static int getRemainDayOfMonth(Date date) {  
  146.         int dayOfMonth = getDayOfMonth(date);  
  147.         int day = getPassDayOfMonth(date);  
  148.         return dayOfMonth - day;  
  149.     }  
  150.   
  151.     /** 
  152.      * 取得月已经过的天数 
  153.      * 
  154.      * @param date 
  155.      * @return 
  156.      */  
  157.     public static int getPassDayOfMonth(Date date) {  
  158.         Calendar c = Calendar.getInstance();  
  159.         c.setTime(date);  
  160.         return c.get(Calendar.DAY_OF_MONTH);  
  161.     }  
  162.   
  163.     /** 
  164.      * 取得月天数 
  165.      * 
  166.      * @param date 
  167.      * @return 
  168.      */  
  169.     public static int getDayOfMonth(Date date) {  
  170.         Calendar c = Calendar.getInstance();  
  171.         c.setTime(date);  
  172.         return c.getActualMaximum(Calendar.DAY_OF_MONTH);  
  173.     }  
  174.   
  175.     /** 
  176.      * 取得月第一天 
  177.      * 
  178.      * @param date 
  179.      * @return 
  180.      */  
  181.     public static Date getFirstDateOfMonth(Date date) {  
  182.         Calendar c = Calendar.getInstance();  
  183.         c.setTime(date);  
  184.         c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));  
  185.         return c.getTime();  
  186.     }  
  187.   
  188.     /** 
  189.      * 取得月最后一天 
  190.      * 
  191.      * @param date 
  192.      * @return 
  193.      */  
  194.     public static Date getLastDateOfMonth(Date date) {  
  195.         Calendar c = Calendar.getInstance();  
  196.         c.setTime(date);  
  197.         c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));  
  198.         return c.getTime();  
  199.     }  
  200.   
  201.     /** 
  202.      * 取得季度第一天 
  203.      * 
  204.      * @param date 
  205.      * @return 
  206.      */  
  207.     public static Date getFirstDateOfSeason(Date date) {  
  208.         return getFirstDateOfMonth(getSeasonDate(date)[0]);  
  209.     }  
  210.   
  211.     /** 
  212.      * 取得季度最后一天 
  213.      * 
  214.      * @param date 
  215.      * @return 
  216.      */  
  217.     public static Date getLastDateOfSeason(Date date) {  
  218.         return getLastDateOfMonth(getSeasonDate(date)[2]);  
  219.     }  
  220.   
  221.     /** 
  222.      * 取得季度天数 
  223.      * @param date 
  224.      * @return 
  225.      */  
  226.     public static int getDayOfSeason(Date date) {  
  227.         int day = 0;  
  228.         Date[] seasonDates  = getSeasonDate(date);  
  229.         for (Date date2 : seasonDates) {  
  230.             day += getDayOfMonth(date2);  
  231.         }  
  232.         return day;  
  233.     }  
  234.   
  235.     /** 
  236.      * 取得季度剩余天数 
  237.      * 
  238.      * @param date 
  239.      * @return 
  240.      */  
  241.     public static int getRemainDayOfSeason(Date date) {  
  242.         return getDayOfSeason(date) - getPassDayOfSeason(date);  
  243.     }  
  244.   
  245.     /** 
  246.      * 取得季度已过天数 
  247.      * 
  248.      * @param date 
  249.      * @return 
  250.      */  
  251.     public static int getPassDayOfSeason(Date date) {  
  252.         int day = 0;  
  253.   
  254.         Date[] seasonDates  = getSeasonDate(date);  
  255.   
  256.         Calendar c = Calendar.getInstance();  
  257.         c.setTime(date);  
  258.         int month = c.get(Calendar.MONTH);  
  259.   
  260.         if(month == Calendar.JANUARY || month == Calendar.APRIL || month == Calendar.JULY || month == Calendar.OCTOBER) {//季度第一个月  
  261.             day = getPassDayOfMonth(seasonDates[0]);  
  262.         } else if(month == Calendar.FEBRUARY || month == Calendar.MAY || month == Calendar.AUGUST || month == Calendar.NOVEMBER) {//季度第二个月  
  263.             day = getDayOfMonth(seasonDates[0]) + getPassDayOfMonth(seasonDates[1]);  
  264.         } else if(month == Calendar.MARCH || month == Calendar.JUNE || month == Calendar.SEPTEMBER || month == Calendar.DECEMBER) {//季度第三个月  
  265.             day = getDayOfMonth(seasonDates[0]) + getDayOfMonth(seasonDates[1]) + getPassDayOfMonth(seasonDates[2]);  
  266.         }  
  267.         return day;  
  268.     }  
  269.   
  270.     /** 
  271.      * 取得季度月 
  272.      * 
  273.      * @param date 
  274.      * @return 
  275.      */  
  276.     public static Date[] getSeasonDate(Date date) {  
  277.         Date[] season = new Date[3];  
  278.   
  279.         Calendar c = Calendar.getInstance();  
  280.         c.setTime(date);  
  281.   
  282.         int nSeason = getSeason(date);  
  283.         if(nSeason == 1) {//第一季度  
  284.             c.set(Calendar.MONTH, Calendar.JANUARY);  
  285.             season[0] = c.getTime();  
  286.             c.set(Calendar.MONTH, Calendar.FEBRUARY);  
  287.             season[1] = c.getTime();  
  288.             c.set(Calendar.MONTH, Calendar.MARCH);  
  289.             season[2] = c.getTime();  
  290.         } else if(nSeason == 2) {//第二季度  
  291.             c.set(Calendar.MONTH, Calendar.APRIL);  
  292.             season[0] = c.getTime();  
  293.             c.set(Calendar.MONTH, Calendar.MAY);  
  294.             season[1] = c.getTime();  
  295.             c.set(Calendar.MONTH, Calendar.JUNE);  
  296.             season[2] = c.getTime();  
  297.         } else if(nSeason == 3) {//第三季度  
  298.             c.set(Calendar.MONTH, Calendar.JULY);  
  299.             season[0] = c.getTime();  
  300.             c.set(Calendar.MONTH, Calendar.AUGUST);  
  301.             season[1] = c.getTime();  
  302.             c.set(Calendar.MONTH, Calendar.SEPTEMBER);  
  303.             season[2] = c.getTime();  
  304.         } else if(nSeason == 4) {//第四季度  
  305.             c.set(Calendar.MONTH, Calendar.OCTOBER);  
  306.             season[0] = c.getTime();  
  307.             c.set(Calendar.MONTH, Calendar.NOVEMBER);  
  308.             season[1] = c.getTime();  
  309.             c.set(Calendar.MONTH, Calendar.DECEMBER);  
  310.             season[2] = c.getTime();  
  311.         }  
  312.         return season;  
  313.     }  
  314.   
  315.     /** 
  316.      * 
  317.      * 1 第一季度  2 第二季度 3 第三季度 4 第四季度 
  318.      * 
  319.      * @param date 
  320.      * @return 
  321.      */  
  322.     public static int getSeason(Date date) {  
  323.   
  324.         int season = 0;  
  325.   
  326.         Calendar c = Calendar.getInstance();  
  327.         c.setTime(date);  
  328.         int month = c.get(Calendar.MONTH);  
  329.         switch (month) {  
  330.             case Calendar.JANUARY:  
  331.             case Calendar.FEBRUARY:  
  332.             case Calendar.MARCH:  
  333.                 season =  1;  
  334.                 break;  
  335.             case Calendar.APRIL:  
  336.             case Calendar.MAY:  
  337.             case Calendar.JUNE:  
  338.                 season =  2;  
  339.                 break;  
  340.             case Calendar.JULY:  
  341.             case Calendar.AUGUST:  
  342.             case Calendar.SEPTEMBER:  
  343.                 season =  3;  
  344.                 break;  
  345.             case Calendar.OCTOBER:  
  346.             case Calendar.NOVEMBER:  
  347.             case Calendar.DECEMBER:  
  348.                 season =  4;  
  349.                 break;  
  350.             default:  
  351.                 break;  
  352.         }  
  353.         return season;  
  354.     }  
  355.   
  356.     public static void main(String[] args) {  
  357.         String strDate = "2010-03-26";  
  358.         Date date = parseDate(strDate);  
  359.         System.out.println(strDate + " 是一年的第几周?" + getWeekOfYear(date));  
  360.         System.out.println(strDate + " 所在周起始结束日期?" + getWeekBeginAndEndDate(date, "yyyy年MM月dd日"));  
  361.         System.out.println(strDate + " 所在周周一是?" + formatDate(getMondayOfWeek(date)));  
  362.         System.out.println(strDate + " 所在周周日是?" + formatDate(getSundayOfWeek(date)));  
  363.   
  364.         System.out.println(strDate + " 当月第一天日期?" + formatDate(getFirstDateOfMonth(date)));  
  365.         System.out.println(strDate + " 当月最后一天日期?" + formatDate(getLastDateOfMonth(date)));  
  366.         System.out.println(strDate + " 当月天数?" + getDayOfMonth(date));  
  367.         System.out.println(strDate + " 当月已过多少天?" + getPassDayOfMonth(date));  
  368.         System.out.println(strDate + " 当月剩余多少天?" + getRemainDayOfMonth(date));  
  369.   
  370.         System.out.println(strDate + " 所在季度第一天日期?" + formatDate(getFirstDateOfSeason(date)));  
  371.         System.out.println(strDate + " 所在季度最后一天日期?" + formatDate(getLastDateOfSeason(date)));  
  372.         System.out.println(strDate + " 所在季度天数?" + getDayOfSeason(date));  
  373.         System.out.println(strDate + " 所在季度已过多少天?" + getPassDayOfSeason(date));  
  374.         System.out.println(strDate + " 所在季度剩余多少天?" + getRemainDayOfSeason(date));  
  375.         System.out.println(strDate + " 是第几季度?" + getSeason(date));  
  376.         System.out.println(strDate + " 所在季度月份?" + formatDate(getSeasonDate(date)[0], "yyyy年MM月") + "/" + formatDate(getSeasonDate(date)[1], "yyyy年MM月") + "/" + formatDate(getSeasonDate(date)[2], "yyyy年MM月"));  
  377.     }  
  378. }  
 
原创粉丝点击