我喜欢将本地时间格式格式化为没有年份的字符串.目前我能够显示包含年份的本地格式:
java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
String dateString = df.format(date);
Run Code Online (Sandbox Code Playgroud)
因此我收到一个时间字符串输出,如
12.03.2012
03/12/2012
Run Code Online (Sandbox Code Playgroud)
对于不同的国家.现在我想得到一个简短的表格
12.03.
03/12
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
谢谢你的帮助!
LocalDate使用特定格式在Java 8中格式化Locale可以像这样实现:
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(myLocale).format(value);
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(myLocale).format(value);
DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(myLocale).format(value);
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(myLocale).format(value);
Run Code Online (Sandbox Code Playgroud)
假设value = LocalDate.now()这将导致:
// myLocale = Locale.ENGLISH
6/30/16
Jun 30, 2016
June 30, 2016
Thursday, June 30, 2016
// myLocale = Locale.GERMAN
30.06.16
30.06.2016
30. Juni 2016
Donnerstag, 30. Juni 2016
// myLocale = new Locale("es", "ES")
30/06/16
30-jun-2016
30 de junio de 2016
jueves 30 de junio de 2016
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,Java决定使用哪个分隔符(" - ",".","/",""等)以及如何对日期元素进行排序(例如,前一个月,反之亦然等) - 在某些语言环境中它可能是那一年第一,等等.
我的问题是:我如何格式化java.time.YearMonth并java.time.MonthDay依赖于Locale上面的例子?
基于这个例子,我希望这样的结果......
......为YearMonth:
// myLocale …Run Code Online (Sandbox Code Playgroud)