Java常用的日期處理類__Java

來源:互聯網
上載者:User
import java.text.SimpleDateFormat;   import java.util.ArrayList;   import java.util.Calendar;   import java.util.Date;   import java.util.GregorianCalendar;   import java.util.LinkedHashMap;     /**   * 日期公用處理類   *    * @author SongJun   * @version 1.3   */  public class DateUtil {       /**       * 解析一個日期之間的所有月份       *        * @param beginDateStr       * @param endDateStr       * @return       */      public static ArrayList getMonthList(String beginDateStr, String endDateStr) {           // 指定要解析的時間格式           SimpleDateFormat f = new SimpleDateFormat("yyyy-MM");           // 返回的月份列表           String sRet = "";             // 定義一些變數           Date beginDate = null;           Date endDate = null;             GregorianCalendar beginGC = null;           GregorianCalendar endGC = null;           ArrayList list = new ArrayList();             try {               // 將字串parse成日期               beginDate = f.parse(beginDateStr);               endDate = f.parse(endDateStr);                 // 設定日曆               beginGC = new GregorianCalendar();               beginGC.setTime(beginDate);                 endGC = new GregorianCalendar();               endGC.setTime(endDate);                 // 直到兩個時間相同               while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {                   sRet = beginGC.get(Calendar.YEAR) + "-"                          + (beginGC.get(Calendar.MONTH) + 1);                   list.add(sRet);                   // 以月為單位,增加時間                   beginGC.add(Calendar.MONTH, 1);               }               return list;           } catch (Exception e) {               e.printStackTrace();               return null;           }       }         /**       * 解析一個日期段之間的所有日期       *        * @param beginDateStr       *            開始日期       * @param endDateStr       *            結束日期       * @return       */      public static ArrayList getDayList(String beginDateStr, String endDateStr) {           // 指定要解析的時間格式           SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");             // 定義一些變數           Date beginDate = null;           Date endDate = null;             Calendar beginGC = null;           Calendar endGC = null;           ArrayList list = new ArrayList();             try {               // 將字串parse成日期               beginDate = f.parse(beginDateStr);               endDate = f.parse(endDateStr);                 // 設定日曆               beginGC = Calendar.getInstance();               beginGC.setTime(beginDate);                 endGC = Calendar.getInstance();               endGC.setTime(endDate);               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");                 // 直到兩個時間相同               while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {                     list.add(sdf.format(beginGC.getTime()));                   // 以日為單位,增加時間                   beginGC.add(Calendar.DAY_OF_MONTH, 1);               }               return list;           } catch (Exception e) {               e.printStackTrace();               return null;           }       }         public static ArrayList getYearList() {           ArrayList list = new ArrayList();           Calendar c = null;           c = Calendar.getInstance();           c.setTime(new Date());           int currYear = Calendar.getInstance().get(Calendar.YEAR);             int startYear = currYear - 5;           int endYear = currYear + 10;           for (int i = startYear; i < endYear; i++) {               list.add(new Integer(i));           }           return list;       }         public static int getCurrYear() {           return Calendar.getInstance().get(Calendar.YEAR);       }         /**       * 得到某一年周的總數       *        * @param year       * @return       */      public static LinkedHashMap getWeekList(int year) {           LinkedHashMap map = new LinkedHashMap();           Calendar c = new GregorianCalendar();           c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);           int count = getWeekOfYear(c.getTime());             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");           String dayOfWeekStart = "";           String dayOfWeekEnd = "";           for (int i = 1; i <= count; i++) {               dayOfWeekStart = sdf.format(getFirstDayOfWeek(year, i));               dayOfWeekEnd = sdf.format(getLastDayOfWeek(year, i));               map.put(new Integer(i), "第"+i+"周(從"+dayOfWeekStart + "至" + dayOfWeekEnd+")");           }           return map;         }              /**       * 得到一年的總周數       * @param year       * @return       */      public static int getWeekCountInYear(int year){           Calendar c = new GregorianCalendar();           c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);           int count = getWeekOfYear(c.getTime());           return count;       }         /**       * 取得當前日期是多少周       *        * @param date       * @return       */      public static int getWeekOfYear(Date date) {           Calendar c = new GregorianCalendar();           c.setFirstDayOfWeek(Calendar.MONDAY);           c.setMinimalDaysInFirstWeek(7);           c.setTime(date);             return c.get(Calendar.WEEK_OF_YEAR);       }         /**       * 得到某年某周的第一天       *        * @param year       * @param week       * @return       */      public static Date getFirstDayOfWeek(int year, int week) {           Calendar c = new GregorianCalendar();           c.set(Calendar.YEAR, year);           c.set(Calendar.MONTH, Calendar.JANUARY);           c.set(Calendar.DATE, 1);             Calendar cal = (GregorianCalendar) c.clone();           cal.add(Calendar.DATE, week * 7);             return getFirstDayOfWeek(cal.getTime());       }         /**       * 得到某年某周的最後一天       *        * @param year       * @param week       * @return       */      public static Date getLastDayOfWeek(int year, int week) {           Calendar c = new GregorianCalendar();           c.set(Calendar.YEAR, year);           c.set(Calendar.MONTH, Calendar.JANUARY);           c.set(Calendar.DATE, 1);             Calendar cal = (GregorianCalendar) c.clone();           cal.add(Calendar.DATE, week * 7);             return getLastDayOfWeek(cal.getTime());       }              /**       * 得到某年某月的第一天       * @param year       * @param month       * @return       */      public static Date getFirestDayOfMonth(int year,int month){           month = month-1;           Calendar   c   =   Calendar.getInstance();              c.set(Calendar.YEAR, year);           c.set(Calendar.MONTH, month);                      int day = c.getActualMinimum(c.DAY_OF_MONTH);             c.set(Calendar.DAY_OF_MONTH, day);           return c.getTime();                  }              /**       * 提到某年某月的最後一天       * @param year       * @param month       * @return       */      public static Date getLastDayOfMonth(int year,int month){           month = month-1;           Calendar   c   =   Calendar.getInstance();              c.set(Calendar.YEAR, year);           c.set(Calendar.MONTH, month);           int day = c.getActualMaximum(c.DAY_OF_MONTH);           c.set(Calendar.DAY_OF_MONTH, day);           return c.getTime();       }         /**       * 取得當前日期所在周的第一天       *        * @param date       * @return       */      public static Date getFirstDayOfWeek(Date date) {           Calendar c = new GregorianCalendar();           c.setFirstDayOfWeek(Calendar.MONDAY);           c.setTime(date);           c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday           return c.getTime();       }         /**       * 取得當前日期所在周的最後一天       *        * @param date       * @return       */      public static Date getLastDayOfWeek(Date date) {           Calendar c = new GregorianCalendar();           c.setFirstDayOfWeek(Calendar.MONDAY);           c.setTime(date);           c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday           return c.getTime();       }     }  


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.