Java SimpleDateFormat – Java日期格式

介紹

Java的SimpleDateFormatDateFormat類別用於日期格式化。它主要用於我們需要顯示或使用Java的日期和時間功能的地方。這兩個類別都存在於com.text包中。

Java DateFormat

  • DateFormat用於根據提供的特定地區設置將日期格式化為String
  • 地區設置用於指定區域和語言,使代碼更貼近用戶。
  • 世界不同地區寫日期的方式不同。例如,31st Dec 2017在印度會寫作31-12-2017,但在美利堅合眾國會寫作12-31-2017
  • DateFormat類別不是同步的,建議為每個線程創建單獨的實例。

A DateFormat object can be created using the getDateInstance() and getTimeInstance() method of the DateFormat class.

Locale loc = new Locale("en", "US");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, loc);

注意:構造函數Locale(String, String)自版本19起已被棄用

Locale loc = new Locale.Builder().setLanguage("en").setRegion("US").build();

getDateInstance方法需要兩個輸入參數。第一個參數指定要使用的DateFormat,而第二個參數是區域。

使用format

DateFormat類別具有一個format方法,負責格式化。

Locale locale = new Locale("fr", "FR");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);

此代碼將以法語(fr)語言和法國(FR)地區格式化日期:

Output
3 janv. 2018

使用getTimeInstance()

為創建DateFormat實例,我們使用getDateInstance()方法。

為執行時間格式,我們需要一個時間實例。我們將使用getTimeInstance()方法獲取時間實例。

Locale locale = new Locale("fr", "FR");
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);

此代碼將以法語(fr)語言和法國(FR)地區格式化時間:

Output
11:03:01

Java SimpleDateFormat

SimpleDateFormat類似於DateFormat。它們之間唯一的主要區別是SimpleDateFormat可用於具有地區支持的格式化(DateString轉換)和解析(StringDate轉換),而DateFormat不支持地區。DateFormat是提供日期格式化和解析的抽象類。而SimpleDateFormat是擴展DateFormat類的具體實現。

SimpleDateFormat可以使用SimpleDateFormat構造函數創建。構造函數是帶有String模式作為參數的參數化構造函數。

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

字符串pattern是用於格式化日期的模式,輸出將以“MM-dd-yyyy”模式生成。

模式

讓我們看一下應用於格式化模式的模式語法。

Letter for Pattern Date or Time component Examples
G Era designator AD
y Year 2018 (yyyy), 18 (yy)
M Month in year July (MMMM), Jul (MMM), 07 (MM)
w Results in week in year 16
W Results in week in month 3
D Gives the day count in the year 266
d Day of the month 09 (dd), 9(d)
F Day of the week in month 4
E Day name in the week Tuesday, Tue
u Day number of week where 1 represents Monday, 2 represents Tuesday and so on 2
a AM or PM marker AM
H Hour in the day (0-23) 12
k Hour in the day (1-24) 23
K Hour in am/pm for 12 hour format (0-11) 0
h Hour in am/pm for 12 hour format (1-12) 12
m Minute in the hour 59
s Second in the minute 35
S Millisecond in the minute 978
z Timezone Pacific Standard Time; PST; GMT-08:00
Z Timezone offset in hours (RFC pattern) -0800
X Timezone offset in ISO format -08; -0800; -08:00

某些字母應該以不同的數量用於不同的結果,例如對於月份:

Type Pattern Example Output
Full Month MMMM July
Abbreviated Month MMM Jul
Numeric Month MM 07

例子

現在讓我們看一下日期和時間的不同格式的一些示例。

Pattern Result
MM/dd/yyyy 01/02/2018
dd-M-yyyy hh:mm:ss 02-1-2018 06:07:59
dd MMMM yyyy 02 January 2018
dd MMMM yyyy zzzz 02 January 2018 India Standard Time
E, dd MMM yyyy HH:mm:ss z Tue, 02 Jan 2018 18:07:59 IST

使用 SimpleDateFormat()

為了解析日期,我們需要使用構造函數創建 SimpleDateFormat 實例,然後使用 format() 方法:

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);

此代碼將輸出“MM-dd-yyyy”:

Output
01-02-2018

此模式生成“年中的月份”,“月中的日期”和“年份”。字符的類型(以及大小寫)和字符的數量都影響字符串。月份將以兩位數表示,日期以兩位數表示,年份以四位數表示。

此代碼將日期“2018年1月2日”顯示為“01-02-2018”。

為了解析時間,我們必須在創建 SimpleDateFormat 實例時更改模式。

String pattern = "HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);

此代碼將輸出“HH:mm:ss.SSSZ”:

Output
13:03:15.454+0530

此模式生成“小時”,“分鐘”,“秒”,“毫秒”和“時區偏移(RFC模式)”。

此代碼將時間顯示為“13:03:15.454+0530”。

使用 parse()

解析是將字串轉換為java.util.Date實例。我們可以使用SimpleDateFormat類的parse()方法將字串解析為日期實例。為了將字串解析為日期,我們需要SimpleDateFormat類的一個實例以及一個字串模式作為該類的建構函式的輸入。

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse("12-01-2018");
System.out.println(date);

這將輸出一個日期(12-01-2018)而沒有指定的時間:

Output
Sat Dec 01 00:00:00 IST 2018

現在讓我們看一下SimpleDateFormat的示例來解析時間。

String pattern = "HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse("22:00:03");
System.out.println(date);

這將輸出一個時間(22:00:03)而沒有指定的日期:

Output
Thu Jan 01 22:00:03 IST 1970

因為我們沒有指定任何日期,所以程式將紀元視為日期(即01-Jan-1970)。

使用Locale

我們已經使用了Locale作為DateFormat的一部分,並且我們已經看到區域是根據地區使用的。

String pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, new Locale("fr", "FR"));
Date date = simpleDateFormat.format(new Date());
System.out.println(date);

此代碼將以法語(fr)語言和法國(FR)地區格式化當前時間:

Output
mardi janvier 2018 14:51:02.354+0530

根據提供的Locale,日期和月份以法語命名 – mardi是“星期二”,janvier是“一月”。

結論

在本文中,您了解了Java的SimpleDateFormatDateFormat

參考:

Source:
https://www.digitalocean.com/community/tutorials/java-simpledateformat-java-date-format