Java 8有新的Date-Time API來處理日期和時間。 我們應該使用新的Java 8 Date-Time API來格式化和解析日期時間值。
如果我們正在編寫與日期和時間相關的新代碼,我們應該使用新的Date-Time API。
此部分適用于使用舊日期和日歷類的舊代碼。
Java庫提供了兩個類來格式化日期:
DateFormat
類是一個抽象類并且我們可以使用 DateFormat
類以預定義的格式來格式化日期。
因為它是抽象的,所以我們不能創(chuàng)建一個 DateFormat
類的實例使用 new
運算符。
我們必須使用它的一個 getXxxInstance()
方法來創(chuàng)建新的實例。Xxx可以是日期,日期時間或時間。
要格式化日期時間值,我們使用 format()
方法 DateFormat
類。
DateFormat類的格式化文本取決于兩件事:
格式的樣式?jīng)Q定了包括多少日期時間信息在格式化的文本
語言環(huán)境確定要使用的語言環(huán)境。
Date Format
類將五個樣式定義為常量:
DEFAULT
格式與 MEDIUM
相同。getInstance()使用 SHORT
。
下表顯示了對于美國區(qū)域設置以不同樣式格式化的相同日期。
樣式 | 格式化日期 |
---|---|
DEFAULT | Mar 27, 2014 |
SHORT | 3/27/14 |
MEDIUM | Mar 26, 2014 |
LONG | March 26, 2014 |
FULL | Sunday, November 2, 2014 |
以下代碼顯示如何以簡體中文格式顯示語言環(huán)境的默認日期,法國和德國。
import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Main { public static void main(String[] args) { Date today = new Date(); // Print date in the default locale format Locale defaultLocale = Locale.getDefault(); printLocaleDetails(defaultLocale); printDate(defaultLocale, today); // Print date in French format printLocaleDetails(Locale.FRANCE); printDate(Locale.FRANCE, today); // Print date in German format. We could also use Locale.GERMANY // instead of new Locale ("de", "DE"). Locale germanLocale = new Locale("de", "DE"); printLocaleDetails(germanLocale); printDate(germanLocale, today); } public static void printLocaleDetails(Locale locale) { String languageCode = locale.getLanguage(); String languageName = locale.getDisplayLanguage(); String countryCode = locale.getCountry(); String countryName = locale.getDisplayCountry(); // Print the locale info System.out.println("Language: " + languageName + "(" + languageCode + "); " + "Country: " + countryName + "(" + countryCode + ")"); } public static void printDate(Locale locale, Date date) { DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale); String formattedDate = formatter.format(date); System.out.println("SHORT: " + formattedDate); formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); formattedDate = formatter.format(date); System.out.println("MEDIUM: " + formattedDate+"\n"); } }
上面的代碼生成以下結果。
java.util.Locale
類包含常見語言環(huán)境的常量。
我們可以使用 Locale.getDefault()
方法獲取系統(tǒng)的默認區(qū)域設置。
要創(chuàng)建自定義日期格式,我們可以使用 SimpleDateFormat
類。
SimpleDateFormat類是對語言環(huán)境敏感的。
它的默認構造函數(shù)創(chuàng)建一個格式化程序,默認日期格式為默認語言環(huán)境。
SimpleDateFormat
類中的 format()
方法執(zhí)行日期格式。
要更改后續(xù)格式化的日期格式,可以通過將新日期格式作為參數(shù)傳遞來使用applyPattern()方法。
import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { SimpleDateFormat simpleFormatter = new SimpleDateFormat("dd/MM/yyyy"); Date today = new Date(); String formattedDate = simpleFormatter.format(today); System.out.println("Today is (dd/MM/yyyy): " + formattedDate); simpleFormatter.applyPattern("MMMM dd, yyyy"); formattedDate = simpleFormatter.format(today); System.out.println("Today is (MMMM dd, yyyy): " + formattedDate); } }
上面的代碼生成以下結果。
更多建議: