JAVA日期查询:季度、月份、星期等时间信息

  1. package com.stt.dateChange;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Calendar;  
  5. import java.util.Date;  
  6.   
  7. import org.apache.commons.logging.Log;  
  8. import org.apache.commons.logging.LogFactory;  
  9.   
  10. /** 
  11.  *  
  12.  * @author zengms 
  13.  * @date 2012-12-27 
  14.  *  
  15.  */  
  16. public class DateUtils {  
  17.   
  18.     private static final Log logger = LogFactory.getLog(DateUtils.class);  
  19.   
  20.     public static final String YYYYMMDD = "yyyy-MM-dd";  
  21.   
  22.     public static final String YYYYMMDD_ZH = "yyyy年MM月dd日";  
  23.   
  24.     public static final int FIRST_DAY_OF_WEEK = Calendar.MONDAY; // 中国周一是一周的第一天  
  25.   
  26.     /** 
  27.      *  
  28.      * @param strDate 
  29.      * @return 
  30.      */  
  31.     public static Date parseDate(String strDate) {  
  32.         return parseDate(strDate, null);  
  33.     }  
  34.   
  35.     /** 
  36.      * parseDate 
  37.      *  
  38.      * @param strDate 
  39.      * @param pattern 
  40.      * @return 
  41.      */  
  42.     public static Date parseDate(String strDate, String pattern) {  
  43.         Date date = null;  
  44.         try {  
  45.             if (pattern == null) {  
  46.                 pattern = YYYYMMDD;  
  47.             }  
  48.             SimpleDateFormat format = new SimpleDateFormat(pattern);  
  49.             date = format.parse(strDate);  
  50.         } catch (Exception e) {  
  51.             logger.error("parseDate error:" + e);  
  52.         }  
  53.         return date;  
  54.     }  
  55.   
  56.     /** 
  57.      * format date 
  58.      *  
  59.      * @param date 
  60.      * @return 
  61.      */  
  62.     public static String formatDate(Date date) {  
  63.         return formatDate(date, null);  
  64.     }  
  65.   
  66.     /** 
  67.      * format date 
  68.      *  
  69.      * @param date 
  70.      * @param pattern 
  71.      * @return 
  72.      */  
  73.     public static String formatDate(Date date, String pattern) {  
  74.         String strDate = null;  
  75.         try {  
  76.             if (pattern == null) {  
  77.                 pattern = YYYYMMDD;  
  78.             }  
  79.             SimpleDateFormat format = new SimpleDateFormat(pattern);  
  80.             strDate = format.format(date);  
  81.         } catch (Exception e) {  
  82.             logger.error("formatDate error:", e);  
  83.         }  
  84.         return strDate;  
  85.     }  
  86.   
  87.     /** 
  88.      * 取得日期:年 
  89.      *  
  90.      * @param date 
  91.      * @return 
  92.      */  
  93.     public static int getYear(Date date) {  
  94.         Calendar c = Calendar.getInstance();  
  95.         c.setTime(date);  
  96.         int year = c.get(Calendar.YEAR);  
  97.         return year;  
  98.     }  
  99.   
  100.     /** 
  101.      * 取得日期:年 
  102.      *  
  103.      * @param date 
  104.      * @return 
  105.      */  
  106.     public static int getMonth(Date date) {  
  107.         Calendar c = Calendar.getInstance();  
  108.         c.setTime(date);  
  109.         int month = c.get(Calendar.MONTH);  
  110.         return month + 1;  
  111.     }  
  112.   
  113.     /** 
  114.      * 取得日期:年 
  115.      *  
  116.      * @param date 
  117.      * @return 
  118.      */  
  119.     public static int getDay(Date date) {  
  120.         Calendar c = Calendar.getInstance();  
  121.         c.setTime(date);  
  122.         int da = c.get(Calendar.DAY_OF_MONTH);  
  123.         return da;  
  124.     }  
  125.   
  126.     /** 
  127.      * 取得当天日期是周几 
  128.      *  
  129.      * @param date 
  130.      * @return 
  131.      */  
  132.     public static int getWeekDay(Date date) {  
  133.         Calendar c = Calendar.getInstance();  
  134.         c.setTime(date);  
  135.         int week_of_year = c.get(Calendar.DAY_OF_WEEK);  
  136.         return week_of_year - 1;  
  137.     }  
  138.   
  139.     /** 
  140.      * 取得一年的第几周 
  141.      *  
  142.      * @param date 
  143.      * @return 
  144.      */  
  145.     public static int getWeekOfYear(Date date) {  
  146.         Calendar c = Calendar.getInstance();  
  147.         c.setTime(date);  
  148.         int week_of_year = c.get(Calendar.WEEK_OF_YEAR);  
  149.         return week_of_year;  
  150.     }  
  151.   
  152.     /** 
  153.      * getWeekBeginAndEndDate 
  154.      *  
  155.      * @param date 
  156.      * @param pattern 
  157.      * @return 
  158.      */  
  159.     public static String getWeekBeginAndEndDate(Date date, String pattern) {  
  160.         Date monday = getMondayOfWeek(date);  
  161.         Date sunday = getSundayOfWeek(date);  
  162.         return formatDate(monday, pattern) + " - "  
  163.                 + formatDate(sunday, pattern);  
  164.     }  
  165.   
  166.     /** 
  167.      * 根据日期取得对应周周一日期 
  168.      *  
  169.      * @param date 
  170.      * @return 
  171.      */  
  172.     public static Date getMondayOfWeek(Date date) {  
  173.         Calendar monday = Calendar.getInstance();  
  174.         monday.setTime(date);  
  175.         monday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);  
  176.         monday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
  177.         return monday.getTime();  
  178.     }  
  179.   
  180.     /** 
  181.      * 根据日期取得对应周周日日期 
  182.      *  
  183.      * @param date 
  184.      * @return 
  185.      */  
  186.     public static Date getSundayOfWeek(Date date) {  
  187.         Calendar sunday = Calendar.getInstance();  
  188.         sunday.setTime(date);  
  189.         sunday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);  
  190.         sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);  
  191.         return sunday.getTime();  
  192.     }  
  193.   
  194.     /** 
  195.      * 取得月的剩余天数 
  196.      *  
  197.      * @param date 
  198.      * @return 
  199.      */  
  200.     public static int getRemainDayOfMonth(Date date) {  
  201.         int dayOfMonth = getDayOfMonth(date);  
  202.         int day = getPassDayOfMonth(date);  
  203.         return dayOfMonth - day;  
  204.     }  
  205.   
  206.     /** 
  207.      * 取得月已经过的天数 
  208.      *  
  209.      * @param date 
  210.      * @return 
  211.      */  
  212.     public static int getPassDayOfMonth(Date date) {  
  213.         Calendar c = Calendar.getInstance();  
  214.         c.setTime(date);  
  215.         return c.get(Calendar.DAY_OF_MONTH);  
  216.     }  
  217.   
  218.     /** 
  219.      * 取得月天数 
  220.      *  
  221.      * @param date 
  222.      * @return 
  223.      */  
  224.     public static int getDayOfMonth(Date date) {  
  225.         Calendar c = Calendar.getInstance();  
  226.         c.setTime(date);  
  227.         return c.getActualMaximum(Calendar.DAY_OF_MONTH);  
  228.     }  
  229.   
  230.     /** 
  231.      * 取得月第一天 
  232.      *  
  233.      * @param date 
  234.      * @return 
  235.      */  
  236.     public static Date getFirstDateOfMonth(Date date) {  
  237.         Calendar c = Calendar.getInstance();  
  238.         c.setTime(date);  
  239.         c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));  
  240.         return c.getTime();  
  241.     }  
  242.   
  243.     /** 
  244.      * 取得月最后一天 
  245.      *  
  246.      * @param date 
  247.      * @return 
  248.      */  
  249.     public static Date getLastDateOfMonth(Date date) {  
  250.         Calendar c = Calendar.getInstance();  
  251.         c.setTime(date);  
  252.         c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));  
  253.         return c.getTime();  
  254.     }  
  255.   
  256.     /** 
  257.      * 取得季度第一天 
  258.      *  
  259.      * @param date 
  260.      * @return 
  261.      */  
  262.     public static Date getFirstDateOfSeason(Date date) {  
  263.         return getFirstDateOfMonth(getSeasonDate(date)[0]);  
  264.     }  
  265.   
  266.     /** 
  267.      * 取得季度最后一天 
  268.      *  
  269.      * @param date 
  270.      * @return 
  271.      */  
  272.     public static Date getLastDateOfSeason(Date date) {  
  273.         return getLastDateOfMonth(getSeasonDate(date)[2]);  
  274.     }  
  275.   
  276.     /** 
  277.      * 取得季度天数 
  278.      *  
  279.      * @param date 
  280.      * @return 
  281.      */  
  282.     public static int getDayOfSeason(Date date) {  
  283.         int day = 0;  
  284.         Date[] seasonDates = getSeasonDate(date);  
  285.         for (Date date2 : seasonDates) {  
  286.             day += getDayOfMonth(date2);  
  287.         }  
  288.         return day;  
  289.     }  
  290.   
  291.     /** 
  292.      * 取得季度剩余天数 
  293.      *  
  294.      * @param date 
  295.      * @return 
  296.      */  
  297.     public static int getRemainDayOfSeason(Date date) {  
  298.         return getDayOfSeason(date) - getPassDayOfSeason(date);  
  299.     }  
  300.   
  301.     /** 
  302.      * 取得季度已过天数 
  303.      *  
  304.      * @param date 
  305.      * @return 
  306.      */  
  307.     public static int getPassDayOfSeason(Date date) {  
  308.         int day = 0;  
  309.   
  310.         Date[] seasonDates = getSeasonDate(date);  
  311.   
  312.         Calendar c = Calendar.getInstance();  
  313.         c.setTime(date);  
  314.         int month = c.get(Calendar.MONTH);  
  315.   
  316.         if (month == Calendar.JANUARY || month == Calendar.APRIL  
  317.                 || month == Calendar.JULY || month == Calendar.OCTOBER) {// 季度第一个月  
  318.             day = getPassDayOfMonth(seasonDates[0]);  
  319.         } else if (month == Calendar.FEBRUARY || month == Calendar.MAY  
  320.                 || month == Calendar.AUGUST || month == Calendar.NOVEMBER) {// 季度第二个月  
  321.             day = getDayOfMonth(seasonDates[0])  
  322.                     + getPassDayOfMonth(seasonDates[1]);  
  323.         } else if (month == Calendar.MARCH || month == Calendar.JUNE  
  324.                 || month == Calendar.SEPTEMBER || month == Calendar.DECEMBER) {// 季度第三个月  
  325.             day = getDayOfMonth(seasonDates[0]) + getDayOfMonth(seasonDates[1])  
  326.                     + getPassDayOfMonth(seasonDates[2]);  
  327.         }  
  328.         return day;  
  329.     }  
  330.   
  331.     /** 
  332.      * 取得季度月 
  333.      *  
  334.      * @param date 
  335.      * @return 
  336.      */  
  337.     public static Date[] getSeasonDate(Date date) {  
  338.         Date[] season = new Date[3];  
  339.   
  340.         Calendar c = Calendar.getInstance();  
  341.         c.setTime(date);  
  342.   
  343.         int nSeason = getSeason(date);  
  344.         if (nSeason == 1) {// 第一季度  
  345.             c.set(Calendar.MONTH, Calendar.JANUARY);  
  346.             season[0] = c.getTime();  
  347.             c.set(Calendar.MONTH, Calendar.FEBRUARY);  
  348.             season[1] = c.getTime();  
  349.             c.set(Calendar.MONTH, Calendar.MARCH);  
  350.             season[2] = c.getTime();  
  351.         } else if (nSeason == 2) {// 第二季度  
  352.             c.set(Calendar.MONTH, Calendar.APRIL);  
  353.             season[0] = c.getTime();  
  354.             c.set(Calendar.MONTH, Calendar.MAY);  
  355.             season[1] = c.getTime();  
  356.             c.set(Calendar.MONTH, Calendar.JUNE);  
  357.             season[2] = c.getTime();  
  358.         } else if (nSeason == 3) {// 第三季度  
  359.             c.set(Calendar.MONTH, Calendar.JULY);  
  360.             season[0] = c.getTime();  
  361.             c.set(Calendar.MONTH, Calendar.AUGUST);  
  362.             season[1] = c.getTime();  
  363.             c.set(Calendar.MONTH, Calendar.SEPTEMBER);  
  364.             season[2] = c.getTime();  
  365.         } else if (nSeason == 4) {// 第四季度  
  366.             c.set(Calendar.MONTH, Calendar.OCTOBER);  
  367.             season[0] = c.getTime();  
  368.             c.set(Calendar.MONTH, Calendar.NOVEMBER);  
  369.             season[1] = c.getTime();  
  370.             c.set(Calendar.MONTH, Calendar.DECEMBER);  
  371.             season[2] = c.getTime();  
  372.         }  
  373.         return season;  
  374.     }  
  375.   
  376.     /** 
  377.      *  
  378.      * 1 第一季度 2 第二季度 3 第三季度 4 第四季度 
  379.      *  
  380.      * @param date 
  381.      * @return 
  382.      */  
  383.     public static int getSeason(Date date) {  
  384.   
  385.         int season = 0;  
  386.   
  387.         Calendar c = Calendar.getInstance();  
  388.         c.setTime(date);  
  389.         int month = c.get(Calendar.MONTH);  
  390.         switch (month) {  
  391.         case Calendar.JANUARY:  
  392.         case Calendar.FEBRUARY:  
  393.         case Calendar.MARCH:  
  394.             season = 1;  
  395.             break;  
  396.         case Calendar.APRIL:  
  397.         case Calendar.MAY:  
  398.         case Calendar.JUNE:  
  399.             season = 2;  
  400.             break;  
  401.         case Calendar.JULY:  
  402.         case Calendar.AUGUST:  
  403.         case Calendar.SEPTEMBER:  
  404.             season = 3;  
  405.             break;  
  406.         case Calendar.OCTOBER:  
  407.         case Calendar.NOVEMBER:  
  408.         case Calendar.DECEMBER:  
  409.             season = 4;  
  410.             break;  
  411.         default:  
  412.             break;  
  413.         }  
  414.         return season;  
  415.     }  
  416.   
  417.     public static void main(String[] args) {  
  418.   
  419.         String strDate = "2013-01-01";  
  420.   
  421.         Date date = parseDate(strDate);  
  422.   
  423.         System.out.println(strDate + " 今天是哪一年?" + getYear(date));  
  424.         System.out.println(strDate + " 今天是哪个月?" + getMonth(date));  
  425.         System.out.println(strDate + " 今天是几号?" + getDay(date));  
  426.         System.out.println(strDate + " 今天是周几?" + getWeekDay(date));  
  427.         System.out.println(strDate + " 是一年的第几周?" + getWeekOfYear(date));  
  428.         System.out.println(strDate + " 所在周起始结束日期?"  
  429.                 + getWeekBeginAndEndDate(date, "yyyy年MM月dd日"));  
  430.         System.out.println(strDate + " 所在周周一是?"  
  431.                 + formatDate(getMondayOfWeek(date)));  
  432.         System.out.println(strDate + " 所在周周日是?"  
  433.                 + formatDate(getSundayOfWeek(date)));  
  434.   
  435.         System.out.println(strDate + " 当月第一天日期?"  
  436.                 + formatDate(getFirstDateOfMonth(date)));  
  437.         System.out.println(strDate + " 当月最后一天日期?"  
  438.                 + formatDate(getLastDateOfMonth(date)));  
  439.         System.out.println(strDate + " 当月天数?" + getDayOfMonth(date));  
  440.         System.out.println(strDate + " 当月已过多少天?" + getPassDayOfMonth(date));  
  441.         System.out.println(strDate + " 当月剩余多少天?" + getRemainDayOfMonth(date));  
  442.   
  443.         System.out.println(strDate + " 所在季度第一天日期?"  
  444.                 + formatDate(getFirstDateOfSeason(date)));  
  445.         System.out.println(strDate + " 所在季度最后一天日期?"  
  446.                 + formatDate(getLastDateOfSeason(date)));  
  447.         System.out.println(strDate + " 所在季度天数?" + getDayOfSeason(date));  
  448.         System.out.println(strDate + " 所在季度已过多少天?" + getPassDayOfSeason(date));  
  449.         System.out  
  450.                 .println(strDate + " 所在季度剩余多少天?" + getRemainDayOfSeason(date));  
  451.         System.out.println(strDate + " 是第几季度?" + getSeason(date));  
  452.         System.out.println(strDate + " 所在季度月份?"  
  453.                 + formatDate(getSeasonDate(date)[0], "yyyy年MM月") + "/"  
  454.                 + formatDate(getSeasonDate(date)[1], "yyyy年MM月") + "/"  
  455.                 + formatDate(getSeasonDate(date)[2], "yyyy年MM月"));  
  456.     }  
posted @ 2016-07-14 15:33  javaweb开发  阅读(1234)  评论(0)    收藏  举报