java Date工具類

  1. /** 
  2.  *  
  3.  * <p> 
  4.  * Title: DateUtil.java 
  5.  * </p> 
  6.  * <p> 
  7.  * Description: 一些日期的工具方法,時間格式全部爲二十四小時制 
  8.  * </p> 
  9.  *  
  10.  */ 
  11. public class DateUtil { 
  12.     // ////////////////////////////////////////////////////////// 
  13.     // /以下是將日期轉換成字符串的方法 
  14.     /** 
  15.      * 返回當前的時間 yyyy-MM-dd HH:mm:ss 
  16.      *  
  17.      * @return 
  18.      */ 
  19.     public static String getNowDateTime() { 
  20.         return DateFormateFactory.getS("datetime").format(new Date()); 
  21.     } 
  22.  
  23.     /** 
  24.      * 將傳入的日期轉換成 yyyy-MM-dd HH:mm:ss的形式 
  25.      *  
  26.      * @param date 
  27.      * @return 
  28.      */ 
  29.     public static String getDateTime(Date date) { 
  30.         return DateFormateFactory.getS("datetime").format(date); 
  31.     } 
  32.  
  33.     /** 
  34.      * 將傳入的long轉換成 yyyy-MM-dd HH:mm:ss的形式 
  35.      *  
  36.      * @param time 
  37.      * @return 
  38.      */ 
  39.     public static String getDateTime(long time) { 
  40.         return DateFormateFactory.getS("datetime").format(time); 
  41.     } 
  42.  
  43.     /** 
  44.      * 返回當前日期 yyyy-MM-dd 
  45.      *  
  46.      * @return 
  47.      */ 
  48.     public static String getNowDate() { 
  49.         return DateFormateFactory.getS("date").format(new Date()); 
  50.     } 
  51.  
  52.     /** 
  53.      * 將傳入的日期轉換成 yyyy-MM-dd的形式 
  54.      *  
  55.      * @param date 
  56.      * @return 
  57.      */ 
  58.     public static String getDate(Date date) { 
  59.         return DateFormateFactory.getS("date").format(date); 
  60.     } 
  61.  
  62.     /** 
  63.      * 將傳入的long轉換成 yyyy-MM-dd的形式 
  64.      *  
  65.      * @param date 
  66.      * @return 
  67.      */ 
  68.     public static String getDate(long time) { 
  69.         return DateFormateFactory.getS("date").format(time); 
  70.     } 
  71.  
  72.     /** 
  73.      * 返回當前的年月 yyyy-MM 
  74.      *  
  75.      * @return 
  76.      */ 
  77.     public static String getNowYearMonth() { 
  78.         return DateFormateFactory.getS("yearmonth").format(new Date()); 
  79.     } 
  80.  
  81.     /** 
  82.      * 將傳入的日期轉換成 yyyy-MM形式 
  83.      *  
  84.      * @param date 
  85.      * @return 
  86.      */ 
  87.     public static String getYearMonth(Date date) { 
  88.         return DateFormateFactory.getS("yearmonth").format(date); 
  89.     } 
  90.  
  91.     /** 
  92.      * 將傳入的long轉換成 yyyy-MM形式 
  93.      *  
  94.      * @param date 
  95.      * @return 
  96.      */ 
  97.     public static String getYearMonth(long time) { 
  98.         return DateFormateFactory.getS("yearmonth").format(time); 
  99.     } 
  100.  
  101.     /** 
  102.      * 得到程序員自定義格式的當前時間字符串 
  103.      *  
  104.      * @param pattern 
  105.      * @return 
  106.      * @throws IllegalArgumentException 
  107.      *             如果輸入的pattern不正確 
  108.      */ 
  109.     public static String getNowByPattern(String pattern) 
  110.             throws IllegalArgumentException { 
  111.         try { 
  112.             return new SimpleDateFormat(pattern).format(new Date()); 
  113.         } catch (IllegalArgumentException e) { 
  114.             throw new IllegalArgumentException("輸入參數不正確", e); 
  115.         } 
  116.     } 
  117.  
  118.     /** 
  119.      * 得到程序員自定義格式的特定時間字符串 
  120.      *  
  121.      * @param pattern 
  122.      * @param date 
  123.      * @return 
  124.      * @throws IllegalArgumentException 
  125.      *             如果輸入的pattern不正確 
  126.      */ 
  127.     public static String getByPattern(String pattern, Date date) 
  128.             throws IllegalArgumentException { 
  129.         try { 
  130.             return new SimpleDateFormat(pattern).format(date); 
  131.         } catch (IllegalArgumentException e) { 
  132.             throw new IllegalArgumentException("輸入參數不正確", e); 
  133.         } 
  134.     } 
  135.  
  136.     /** 
  137.      * 得到程序員自定義格式的特定long時間的字符串 
  138.      *  
  139.      * @param pattern 
  140.      * @param date 
  141.      * @return 
  142.      * @throws IllegalArgumentException 
  143.      *             如果輸入的pattern不正確 
  144.      */ 
  145.     public static String getByPattern(String pattern, long time) 
  146.             throws IllegalArgumentException { 
  147.         try { 
  148.             return new SimpleDateFormat(pattern).format(time); 
  149.         } catch (IllegalArgumentException e) { 
  150.             throw new IllegalArgumentException("輸入參數不正確", e); 
  151.         } 
  152.     } 
  153.  
  154.     // ///////////////////////////////////////////////////// 
  155.     // 以下是將字符串轉換成日期的方法,一般用於解析界面上傳入的日期數據 
  156.     /** 
  157.      * 將格式爲 yyyy-MM-dd 形式的字符串轉換成日期 
  158.      */ 
  159.     public static Date parseDate(String sourse) throws Exception { 
  160.         try { 
  161.             return DateFormateFactory.getS("date").parse(sourse); 
  162.         } catch (ParseException e) { 
  163.             throw new Exception("期待 yyyy-MM-dd 格式的日期,但輸入格式爲: " + sourse, e); 
  164.         } 
  165.     } 
  166.  
  167.     /** 
  168.      * 將格式爲 yyyy-MM-dd HH:mm:ss 的字符串轉換成日期 
  169.      *  
  170.      * @param sourse 
  171.      * @return 
  172.      * @throws Exception 
  173.      */ 
  174.     public static Date parseDateTime(String sourse) throws Exception { 
  175.         try { 
  176.             return DateFormateFactory.getS("datetime").parse(sourse); 
  177.         } catch (ParseException e) { 
  178.             throw new Exception("期待 yyyy-MM-dd HH:mm:ss  格式的日期,但輸入格式爲: " 
  179.                     + sourse, e); 
  180.         } 
  181.     } 
  182.  
  183.     /** 
  184.      * 將傳入的時間間隔(毫秒)轉換成 xx天xx小時xx分xx秒的形式 
  185.      *  
  186.      * @param distance 
  187.      * @return 
  188.      * @deprecated 因爲暫時沒有實現,所以就不推薦使用了 ^.^ 
  189.      */ 
  190.     public static String getDistance(long distance) { 
  191.  
  192.         return null
  193.     } 
  194.  
  195.     /** 
  196.      * 得到與傳入時間date,在字段offsetField上相差offset的date 
  197.      *  
  198.      * @param date 
  199.      * @param offset 
  200.      * @param offsetField 
  201.      *            Calendar類的field 
  202.      * @return 
  203.      */ 
  204.     public static Date getDistanceDate(Date date, int offset, int offsetField) { 
  205.         Calendar c = Calendar.getInstance(); 
  206.         c.setTime(date); 
  207.         c.set(offsetField, c.get(offsetField) + offset); 
  208.         return c.getTime(); 
  209.     } 
  210.  
  211.     /** 
  212.      * 得到兩個日期之間查幾天。 
  213.      * @param date1 
  214.      * @param date2 
  215.      * @return 
  216.      */ 
  217.     public static int getOffsetIn2Date(Date date1, Date date2) { 
  218.         long offsetNum = date1.getTime() - date2.getTime(); 
  219.         return (int) (offsetNum / (60 * 60 * 24 * 1000)); 
  220.     } 
  221.      
  222.     /** 
  223.      * 兩天之前差多少分鐘 
  224.      * @param date1 
  225.      * @param date2 
  226.      * @return 
  227.      */ 
  228.     public static int getOffsetMinIn2Date(Date date1, Date date2){ 
  229.         long offsetNum = date1.getTime() - date2.getTime(); 
  230.         return ((int) (offsetNum / (60 * 1000))); 
  231.     } 
  232.  
  233.     public static void main(String[] args) throws Exception { 
  234.         Date date = getDistanceDate(new Date(), -1, Calendar.DAY_OF_MONTH); 
  235.         System.out.println(getByPattern("yyyyMMdd", date)); 
  236.     } 
  237.  
  238.  
  239. class DateFormateFactory { 
  240.     private static Map<String, SimpleDateFormat> map = new HashMap<String, SimpleDateFormat>(); 
  241.  
  242.     static { 
  243.         map.put("datetime"new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); 
  244.         map.put("date"new SimpleDateFormat("yyyy-MM-dd")); 
  245.         map.put("yearmonth"new SimpleDateFormat("yyyy-MM")); 
  246.         map.put("date1"new SimpleDateFormat("yyyyMMdd")); 
  247.     } 
  248.  
  249.     protected static SimpleDateFormat getS(String formate) { 
  250.         return map.get(formate); 
  251.     } 
  252.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章