2

I need to be able to support nominative month names in the format month year e.g. Listopad 2016 in Polish. I am still supporting the genitive format of dates elsewhere as complete MMM/dd/YYYY dates so I don't want to lose that functionality. I am using Java 8 and I believe that Java 8 by default uses the genitive form.

I've tried using jodaTime to MonthYear, but the Java 8 update appears to be forcing it to show the genitive form everywhere. I am going to need to support other languages that support declensions for months such as slovak, czech, etc.

Any suggestions would be appreciated!

ntalbs
  • 28,700
  • 8
  • 66
  • 83
latimko
  • 23
  • 3
  • 1
    Are you looking for the `L` pattern? It does output "listopad" when asked for November in Polish, see also http://stackoverflow.com/questions/23739718/getting-nominative-month-name-in-jodatime – Tunaki Nov 15 '16 at 21:51

1 Answers1

4

You can use DateTimeFormatter with withLocale. Refer to the following code:

DateTimeFormatter df_en = DateTimeFormatter.ofPattern("MMMM/dd/yyyy").withLocale(Locale.ENGLISH)
DateTimeFormatter df_pl = DateTimeFormatter.ofPattern("MMMM/dd/yyyy").withLocale(new Locale("pl"))
DateTimeFormatter df_cs = DateTimeFormatter.ofPattern("MMMM/dd/yyyy").withLocale(new Locale("cs"))
DateTimeFormatter df_sk = DateTimeFormatter.ofPattern("MMMM/dd/yyyy").withLocale(new Locale("sk"))

LocalDate d = LocalDate.now()
=> java.time.LocalDate d = 2016-11-15
d.format(df_en)
=> "November/15/2016"
d.format(df_pl)
=> "listopada/15/2016"
d.format(df_cs)
=> "listopadu/15/2016"
d.format(df_sk)
=> "novembra/15/2016"
ntalbs
  • 28,700
  • 8
  • 66
  • 83
  • 1
    Which prints "listopada", not "listopad". The difference is the standalone vs standard form. – Tunaki Nov 15 '16 at 22:31
  • 5
    If you replace `MMMM` with `LLLL`, then the result will be `listopad`. – ntalbs Nov 15 '16 at 22:40
  • I believe a combination of the format LLLL and using the DateTimeFormatter withLocale will resolve my issue. Thanks! – latimko Nov 17 '16 at 15:14
  • You can also use [`Month::getDisplayName`](https://docs.oracle.com/javase/10/docs/api/java/time/Month.html#getDisplayName(java.time.format.TextStyle,java.util.Locale)) and pass a [`TextStyle`](https://docs.oracle.com/javase/10/docs/api/java/time/format/TextStyle.html) for either the standalone or combo version such as `TextStyle.FULL` or `TextStyle.FULL_STANDALONE`. – Basil Bourque Sep 21 '18 at 04:00