java編程--日期格式化

來源:互聯網
上載者:User

標籤:public   blog   instance   color   格式化   turn   esc   sage   span   

第二篇,介紹日期的格式化

 日期的格式化,在日常開發中也經常會用到。例如:

1.指定新舊日期格式,格式一個字串日期。關鍵是是使用,SimpleDateFormat 對象的format()方法

 1     // 日期全格式 2     public static String DATE_FORMAT = "yyyy-MM-dd"; 3     public static String DATE_SLASH_FORMAT = "yyyy/MM/dd"; 4      5 /** 6      * 根據新舊日期格式來格式化日期 7      * @param dateStr 8      * @param formatOld 9      * @param formartNew10      * @return11      * @Description:12      */13     public String convertDateString(String dateStr,String formatOld,String formartNew){14         try {15             SimpleDateFormat sdfNew = new SimpleDateFormat(formartNew);16             SimpleDateFormat sdfOld = new SimpleDateFormat(formatOld);17             Date d1 = sdfOld.parse(dateStr);18             return sdfNew.format(d1);19         } catch (ParseException e) {20             return "";21         }22     }23     24     /**25      * 測試26      * @param args27      * @Description:28      */29     @Test30     public void testConvertDateString(){31         String dateStr = "2017/10/31";32         String newDateStr = convertDateString(dateStr, DateHelper.DATE_SLASH_FORMAT,DateHelper.DATE_FORMAT);33         System.out.println(newDateStr);34     }

2.把一個日期文字按指定的時間格式,返回一個Date對象。關鍵是使用SimpleDateFormat 對象的parse()方法。

    public static String DATE_SLASH_FORMAT = "yyyy/MM/dd";/** * 字串轉日期對象 * @param dateStr * @param pattern * @return * @Description: */public Date converStr2Date(String dateStr,String pattern){try {SimpleDateFormat sdf = new SimpleDateFormat(pattern);return sdf.parse(dateStr);} catch (ParseException e) {return null;}}/** * 測試 * @param args * @Description: */@Testpublic void testConverStr2Date(){String dateStr = "2017/10/31";Date str2Date = converStr2Date(dateStr, DateHelper.DATE_SLASH_FORMAT);System.out.println(str2Date);}

 

3.把字串日期按指定格式格式化,並返回Calendar對象。關鍵是先把字串轉為Date對象,然後轉為使用Calendar的setTime方法。

 1     /** 2      * 字串轉Calendar對象 3      * @param inputStr 4      * @param pattern 5      * @return 6      * @Description: 7      */ 8     public static Calendar cvtStr2Calender(String inputStr,String pattern){ 9         try {10             SimpleDateFormat sdf = new SimpleDateFormat(pattern);11             Date inputDate = sdf.parse(inputStr);12             Calendar cal = Calendar.getInstance();13             cal.setTime(inputDate);14             return cal;15         } catch (ParseException e) {16             log.error(e.getMessage());17             return null;18         }19         20     }

 

  

java編程--日期格式化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.