問題描述
使用 DateFormat,即我可以將當??前日期格式化為短本地化日期,如下所示:
It is quite easy to format and parse Java Date (or Calendar) classes using instance of DateFormat, i.e. I could format current date into short localize date like this:
DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
String today = formatter.format(new Date());
我的問題是:我需要獲取這個本地化的模式字符串(即像 "MM/dd/yy"
之類的東西).這應該是一項微不足道的任務,但我就是找不到提供者...
My problem is: I need to obtain this localized pattern string (i.e. something like "MM/dd/yy"
). This should be a trivial task, but I just couldn't find the provider...
推薦答案
For SimpleDateFormat,你調用 toLocalizedPattern()
對于 Java 8 用戶:
For Java 8 users:
Java 8 日期時間 API 類似于 Joda-time.為了獲得本地化模式,我們可以使用類DateTimeFormatter
The Java 8 Date Time API is similar to Joda-time. To gain a localized pattern we can use class DateTimeFormatter
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
請注意,當您在 LocalDate 上調用 toString() 時,您將獲得 ISO-8601 格式的日期
Note that when you call toString() on LocalDate, you will get date in format ISO-8601
請注意,Java 8 中的日期時間 API 受到 Joda Time 的啟發,大多數解決方案都可以基于與時間相關的問題.
Note that Date Time API in Java 8 is inspired by Joda Time and most solution can be based on questions related to time.
這篇關于如何獲取本地化的日期模式字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!