Java時間相關內容學習(5) SimpleDateFormat介紹

來源:互聯網
上載者:User

介紹SimpleDateFormat。

SimpleDateFormat 介紹

SimpleDateFormat 是一個格式化Date 以及 解析日期文字 的工具。它的最常用途是,能夠按照指定的格式來對Date進行格式化,然後我們使用可以格式化Date後得到的字串。

更嚴格的說,SimpleDateFormat 是一個以與語言環境有關的方式來格式化和解析日期的具體類。它允許進行格式化(日期 -> 文本)、解析(文本 -> 日期)和正常化。

SimpleDateFormat的建構函式:

// 建構函式SimpleDateFormat()SimpleDateFormat(String pattern)SimpleDateFormat(String template, DateFormatSymbols value)SimpleDateFormat(String template, Locale locale)     // 非建構函式void                             applyLocalizedPattern(String template)void                             applyPattern(String template)Object                           clone()boolean                          equals(Object object)StringBuffer                     format(Date date, StringBuffer buffer, FieldPosition fieldPos)AttributedCharacterIterator      formatToCharacterIterator(Object object)Date                             get2DigitYearStart()DateFormatSymbols                getDateFormatSymbols()int                              hashCode()Date                             parse(String string, ParsePosition position)void                             set2DigitYearStart(Date date)void                             setDateFormatSymbols(DateFormatSymbols value)String                           toLocalizedPattern()String                           toPattern()

SimpleDateFormat 簡單示範:

// 建立date對象,時間是2013-09-19

Date date = new Date(113,8,19);

// 建立“SimpleDateFormat對象”,並設定 sdf 的“格式化模式”

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

// 用 sdf 格式化 date,並返回字串。

String str = sdf.format(date);

SimpleDateFormat 相關格式說明

日期和時間模式

日期和時間格式由日期和時間模式 字串指定。在日期和時間模式字串中,未加引號的字母 'A' 到 'Z' 和 'a' 到 'z' 被解釋為模式字母,用來表示日期或時間字串元素。文本可以使用單引號 (') 引起來,以免進行解釋。"''" 表示單引號。所有其他字元均不解釋;只是在格式化時將它們簡單複製到輸出字串,或者在解析時與輸入字串進行匹配。

定義了以下模式字母(所有其他字元 'A' 到 'Z' 和 'a' 到 'z' 都被保留):

聯繫我們

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