Standalone month name form in Java date format

I need to format an incomplete date only to the nearest month. My first assumption was to apply the MMMM yyyy template to it. This is great for many locales (English, German, ...), but gives an ugly result for languages ​​with flex, for example. Russian. I read the SimpleDateFormat documentation and found that "L" should indicate the name of the month, taking into account the insensitivity to the context: only what I need, since there is no day of the month in the template. So I tried the "LLLL yyyy" template. It works great for Russian and other bending languages, but instead does not work, for example. English and German ...

Here is the test code:

 import java.text.*; import java.util.*; public class test { public static void main (String[] arguments) throws Exception { for (String locale : new String[] { "en", "de", "hu", "it", "ru", "fi", "pl" }) System.out.format ("%s: %s \t%s\n", locale, new SimpleDateFormat ("MMMM yyyy", new Locale (locale)).format (new Date ()), new SimpleDateFormat ("LLLL yyyy", new Locale (locale)).format (new Date ())); } } 

And his conclusion:

 en: September 2015 0009 2015 de: September 2015 0009 2015 hu: szeptember 2015 szeptember 2015 it: settembre 2015 Settembre 2015 ru:  2015  2015 fi: syyskuuta 2015 syyskuu 2015 pl: września 2015 wrzesień 2015 

So, for the tested locales: "en" and "de" work correctly only with "M", "hu" works great for both "M" and "L", "this is probably better with" L '(I don’t know , how important is the capital letter), while "ru", "fi" and "pl" give the correct result only with "L" (I really can only say this for Russian and Polish, but I suppose, similarly in Finnish language).

Questions:

  • Can I do something to make SimpleDateFormat or a similar date formatting class handle “L” correctly for all locales?

  • Alternatively, is there an easy way to determine whether to use "M" or "L" for any given language?

+6
source share
2 answers

This is an official error - JDK-8075548

The error is in the "resolved" status, and you can see that the patch was addressed to versions 8_60 and 8_65, but not to earlier versions of Java 8.

Therefore, the correct solution, if possible, is to switch to Java 1.8.0_60.

+3
source

java.time

The SimpleDateFormat class is part of the problematic old time classes, which are now deprecated, are being superseded by the java.time classes.

In java.time, formatting pattern codes are similar to the old ones, but exactly the same. Be sure to check out the doc class for DateTimeFormatter .

 final DateTimeFormatter fCombo = DateTimeFormatter.ofPattern ( "MMMM uuuu" ); final DateTimeFormatter fStandalone = DateTimeFormatter.ofPattern ( "LLLL uuuu" ); final LocalDate localDate = LocalDate.now ( ZoneId.of ( "America/Montreal" ) ); for ( final String locale : new String[] { "en" , "fr" , "de" , "hu" , "it" , "ru" , "fi" , "pl" } ) { final Locale l = new Locale ( locale ); System.out.format ( "%s: %s | %s\n", locale, localDate.format ( fCombo.withLocale ( l ) ), localDate.format ( fStandalone.withLocale ( l ) ) ); } 

ru: March 2017 | 3 2017

fr: mars 2017 | 3 2017

de: März 2017 | 3 2017

hu: március 2017 | március 2017

it: marzo 2017 | Marzo 2017

ru: March 2017 | March 2017

fi: maaliskuuta 2017 | maaliskuu 2017

pl: marca 2017 | marzec 2017


About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

0
source

All Articles