我需要以特定的语言环境格式(localeIreland)打印存储日期(documentDate),到目前为止,我只能使用cal.getDate()来打印今天的日期是否可以使用存储的日期值打印日历?
String documentDate = "03/03/2018";
String pattern = "EEEEE MMMMM yyyy";
SimpleDateFormat formatIreland= new SimpleDateFormat(pattern, localeIreland);
Calendar cal = Calendar.getInstance();
System.out.println(formatIreland.format(documentDate));
这只会打印出今天的日期。
System.out.println(formatIreland.format(cal.getDate()));
DateTimeFormatter docDateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
String documentDate = "03/03/2018";
DateTimeFormatter formatIreland = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withLocale(localeIreland);
System.out.println(LocalDate.parse(documentDate, docDateFormatter).format(formatIreland));
在我的电脑上,这只是印刷
2018年3月3日星期六
将日期格式从一个字符串更改为另一个字符串是一个两步过程,通常需要两个格式化程序:使用一个格式化程序将原始字符串解析为日期时间对象(此处为LocalDate
),然后使用第二个格式化程序将格式化为期望的格式。
格式化特定区域设置的推荐方法是通过内置的区域设置特定格式。这就是DateTimeFormatter.ofLocalizedDate
和withLocale
的组合给你的。
如果您的爱尔兰用户坚持,您当然可以像在问题中一样指定一种格式:
String pattern = "EEEE MMMM yyyy";
DateTimeFormatter formatIreland = DateTimeFormatter.ofPattern(pattern, localeIreland);
通过此更改,我的计算机上的输出是
2018年3月星期六
(对我来说,没有月份的日子,它看起来有点滑稽,但你可以按照自己想要的方式使用它。)
我正在使用并热烈推荐java.time,即现代Java日期和时间API。您使用的日期时间类SimpleDateFormat
和Calendar
已经过时了,前者尤其出了名的麻烦。现代课程使用起来非常好。 LocalDate
似乎完全符合您的要求,因为它是一个没有时间的日期。
链接:Oracle tutorial: Date Time解释如何使用java.time
。
accepted Answer by Ole V.V.是正确的。
如果要仔细阅读所有已知时区的所有本地化格式,可以使用以下代码将示例值转储到控制台。
使用DateTimeFormatter
自动本地化。要进行本地化,请指定:
FormatStyle
确定字符串的长度或缩写。Locale
确定(a)翻译日期名称,月份名称等的人类语言,以及(b)决定缩写,大写,标点符号,分隔符等问题的文化规范。例:
Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = localDate.format( f );
指定所有格式样式。
EnumSet.allOf( FormatStyle.class )
[全,长,中,短]
收集你想要尝试的Locale
对象。您可以获得所有已知语言环境的列表。
Locale[] locales = Locale.getAvailableLocales();
或者只指定几个区域设置。
List < Locale > locales = new ArrayList <>( 3 );
locales.add( Locale.CANADA_FRENCH ); // French, Québec Canada
locales.add( new Locale( "ga" , "IE" ) ); // Irish, Ireland
locales.add( new Locale( "en" , "IE" ) ); // English, Ireland
locales.add( Locale.US ); // English, United States
完整的示例代码。
LocalDate ld = LocalDate.of( 2018 , Month.MARCH , 23 );
List < Locale > locales = new ArrayList <>( 3 );
locales.add( Locale.CANADA_FRENCH );
locales.add( new Locale( "ga" , "IE" ) );
locales.add( new Locale( "en" , "IE" ) );
locales.add( Locale.US );
for ( Locale locale : locales )
{
System.out.println( "------| LOCALE: " + locale + " — " + locale.getDisplayName() + " |----------------------------------" + System.lineSeparator() );
for ( FormatStyle style : EnumSet.allOf( FormatStyle.class ) )
{
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( style ).withLocale( locale );
String output = ld.format( f );
System.out.println( output );
}
System.out.println( "" );
}
System.out.println( "« fin »" + System.lineSeparator() );
我们的几个语言环境的输出。
------| LOCALE: fr_CA — French (Canada) |----------------------------------
vendredi 23 mars 2018
23 mars 2018
23 mars 2018
18-03-23
------| LOCALE: ga_IE — Irish (Ireland) |----------------------------------
Dé hAoine 23 Márta 2018
23 Márta 2018
23 Márta 2018
23/03/2018
------| LOCALE: en_IE — English (Ireland) |----------------------------------
Friday 23 March 2018
23 March 2018
23 Mar 2018
23/03/2018
------| LOCALE: en_US — English (United States) |----------------------------------
Friday, March 23, 2018
March 23, 2018
Mar 23, 2018
3/23/18
« fin »
所有736语言环境中前8个的输出(在Java 9.0.4中)。 (对于Stack Overflow,文本中大约120K的全部内容太多了。)
------| LOCALE: — |----------------------------------
2018 Mar 23, Fri
2018 Mar 23
2018 Mar 23
2018-03-23
------| LOCALE: nn — Norwegian Nynorsk |----------------------------------
fredag 23. mars 2018
23. mars 2018
23. mars 2018
23.03.2018
------| LOCALE: ar_JO — Arabic (Jordan) |----------------------------------
الجمعة، 23 آذار، 2018
23 آذار، 2018
23/03/2018
23/3/2018
------| LOCALE: bg — Bulgarian |----------------------------------
петък, 23 март 2018 г.
23 март 2018 г.
23.03.2018 г.
23.03.18 г.
------| LOCALE: kea — Kabuverdianu |----------------------------------
sesta-fera, 23 di Marsu di 2018
23 di Marsu di 2018
23 Mar 2018
23/3/2018
------| LOCALE: zu — Zulu |----------------------------------
ULwesihlanu, Mashi 23, 2018
Mashi 23, 2018
Mas 23, 2018
3/23/18
------| LOCALE: am_ET — Amharic (Ethiopia) |----------------------------------
ዓርብ ፣23 ማርች 2018
23 ማርች 2018
23 ማርች 2018
23/03/2018
------| LOCALE: fr_DZ — French (Algeria) |----------------------------------
vendredi 23 mars 2018
23 mars 2018
23 mars 2018
23/03/2018
…