Die in Java 8 und höher integrierten Klassen java.time bieten die Klassen MonthDay
und YearMonth
. Ihre toString
und parse
Methoden verwenden Standard ISO 8601 Formate (--MM-DD
& YYYY-MM
), die weise ist.Lokalisiere Strings von `MonthDay` oder` YearMonth` Klassen in java.time?
Für die Präsentation an Menschen sind die Standardformate möglicherweise nicht geeignet. Gibt es trotzdem eine automatisch lokalisierte Zeichenfolge, die die Werte in MonthDay
oder YearMonth
darstellt?
Zum Beispiel, in den USA möchten Benutzer in der Regel MM/DD für Monat-Tag und MM/YY für Jahr-Monat. Während in Großbritannien Benutzer DD/MM für Monat-Tag möchten.
Wie auch immer, um solche Variationen durch Locale
zu automatisieren anstatt Formatierungsmuster explizit zu definieren?
Ich habe den folgenden Code versucht, mit einem datumsorientierten Lokalisierung Formatierer.
Locale l = Locale.US;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate (FormatStyle.SHORT).withLocale (l);
YearMonth ym = YearMonth.of (2017 , Month.JANUARY);
MonthDay md = MonthDay.of (Month.JANUARY , 29);
String outputYm = ym.format (f);
String outputMd = md.format (f);
Dieser Code fehlschlägt, eine Ausnahme auszulösen, wenn entweder YearMonth
oder MonthDay
verwendet.
Für YearMonth
:
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
at java.time.YearMonth.getLong(YearMonth.java:494)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2540)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4347)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
at java.time.YearMonth.format(YearMonth.java:1073)
at javatimestuff.App.doIt(App.java:56)
at javatimestuff.App.main(App.java:45)
Für MonthDay
:
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
at java.time.MonthDay.getLong(MonthDay.java:451)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2540)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4347)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
at java.time.MonthDay.format(MonthDay.java:646)
at javatimestuff.App.doIt(App.java:57)
at javatimestuff.App.main(App.java:45)
Vermutlich suchen Sie etwas im Zusammenhang mit 'MonthDay.format (DateTimeFormatter)'? –
@LouisWasserman Code hinzugefügt, um zu zeigen, dass "DateTimeFormatter.ofLocalizedDate" in den 'format()' Methoden von 'MonthDay' und' YearMonth' fehlschlägt. Wenn Sie eine funktionierende Variante haben, schreiben Sie bitte. Ich frage mich, ob eine Antwort in [DateTimeFormatterBuilder'] (https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html) liegen könnte, aber ich weiß nicht wie. –