我找不到有关 FormatStyle
特定行为的文档设置 DateTimeFormatter
的日期部分和时间部分当 automatically localizing :
DateTimeFormatter.ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle)
是否有某种方法可以了解为任何特定Locale
生成的字符串的可能变体?
最佳答案
如果你想知道生成字符串的所有结果(format
方法的最终结果),那么your answer很好地覆盖了它。我只需在所有区域设置中添加另一个外部循环,因为不同的区域设置也可以有不同的格式样式。
如果您想了解模式,您可以使用DateTimeFormatterBuilder.getLocalizedDateTimePattern
method在相同的嵌套循环内。我还使用了 values()
方法而不是 EnumSet
(不确定它是否有区别):
for (Locale locale : Locale.getAvailableLocales()) {
System.out.println("--|For locale " + locale + "|-------");
for (FormatStyle styleDate : FormatStyle.values()) {
for (FormatStyle styleTime : FormatStyle.values()) {
String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(styleDate, styleTime, IsoChronology.INSTANCE, locale);
int padSpacesOutTo = 17;
String styles = styleDate + " | " + styleTime;
String stylesLabel = String.format("%1$-" + padSpacesOutTo + "s", styles);
System.out.println(stylesLabel + pattern);
}
}
}
输出将是这样的(取决于 JVM 中所有可用的区域设置):
--|For locale ar_AE|-------
FULL | FULL dd MMMM, yyyy z hh:mm:ss a
FULL | LONG dd MMMM, yyyy z hh:mm:ss a
FULL | MEDIUM dd MMMM, yyyy hh:mm:ss a
FULL | SHORT dd MMMM, yyyy hh:mm a
LONG | FULL dd MMMM, yyyy z hh:mm:ss a
LONG | LONG dd MMMM, yyyy z hh:mm:ss a
LONG | MEDIUM dd MMMM, yyyy hh:mm:ss a
LONG | SHORT dd MMMM, yyyy hh:mm a
MEDIUM | FULL dd/MM/yyyy z hh:mm:ss a
MEDIUM | LONG dd/MM/yyyy z hh:mm:ss a
MEDIUM | MEDIUM dd/MM/yyyy hh:mm:ss a
MEDIUM | SHORT dd/MM/yyyy hh:mm a
SHORT | FULL dd/MM/yy z hh:mm:ss a
SHORT | LONG dd/MM/yy z hh:mm:ss a
SHORT | MEDIUM dd/MM/yy hh:mm:ss a
SHORT | SHORT dd/MM/yy hh:mm a
--|For locale ar_JO|-------
FULL | FULL dd MMMM, yyyy z hh:mm:ss a
FULL | LONG dd MMMM, yyyy z hh:mm:ss a
FULL | MEDIUM dd MMMM, yyyy hh:mm:ss a
... lots of output
关于java - 如何在 Java 中查看本地化 `FormatStyle` 的 `DateTimeFormatter` 的每个排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46163948/