4

I have a problem with get a name of month in my language - Czech.

I want to get name of month as noun, but i always get inflected title. I tried many ways to get name, but all ways return inflected title of month name

For example I want to get name of october in czech "Říjen", but i get always "října".

    Calendar cal=Calendar.getInstance();
    SimpleDateFormat month_date = new SimpleDateFormat("MMMMMMMMM");
    String month_name = month_date.format(cal.getTime());
    DateFormat formatData = new SimpleDateFormat("d.MMMM yyyy H:mm");
    System.out.println(month_name);
    System.out.println(String.format(Locale.getDefault(),"%tB", cal));
    System.out.println(formatData.format(cal.getTime()));

All returns bad format for me. Is there any way to get right name of month? Thank you

Vanama
  • 507
  • 1
  • 7
  • 15

4 Answers4

5

You may solved this already, but here is the solution. I made a little research and the reason is the change in java.text.DateFormatSymbols in Java 8 (see compatibility guide).

There is a difference between formatting and standalone forms. Formatting forms: ledna, února, března, etc.; Standalone forms: leden, únor, březen, etc.

Also according to rules of the language the name of month when it stands alone starts with lower character, unless it begins the sentence.

This code will provide you the desired result.

public static void main(String[] args) {
  Calendar cal = Calendar.getInstance();
  Locale loc = new Locale("cs", "CZ"); 
  System.out.println(cal.getDisplayName(Calendar.MONTH, Calendar.LONG_STANDALONE, loc));
}
MikeX
  • 51
  • 1
  • 1
  • This solution works. However, Calendar.LONG_STANDALONE (also SHORT_STANDALONE etc.) works only for API 26+. Is there somebody who knows how to make standalone months work for lower API's? – Tom Wayne May 21 '17 at 12:33
  • 1
    @PepaHruška Add the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) library to your Android library. That is an adaptation for Android of the [*ThreeTen-Backport*](http://www.threeten.org/threetenbp/) project which is a back-port of much of the java.time functionality built into Java 8 and later. Call [`Month::getDisplayName`](http://www.threeten.org/threetenbp/apidocs/org/threeten/bp/Month.html#getDisplayName-org.threeten.bp.format.TextStyle-java.util.Locale-) to localize in either the standalone or combined formats. – Basil Bourque May 21 '17 at 17:25
3

Using this sample code

public static void main(String[] args) {
    Calendar cal= Calendar.getInstance();
    Locale loc = new Locale("cs", "CZ"); 
    System.out.println(String.format(loc,"%tB", cal));
}

I receive output

?íjen

Which looks like what you want (I explicitly defined your Locale).

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • 1
    I received "října" with this solution. Can be affected with android device or android API version? I have Huawei G300 with android 4.03 – Vanama Oct 19 '13 at 13:00
2

In the end, I find the best solution for this problem. The problem is that "MMMM" format is used for "formatted name" of month (f.e. "ledna"). If you need a "standalone name" of month (f.e. "leden"), you need to use "LLLL" instead. Complete date format documentation can be found here:

https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

Here is simple example:

DateFormat.format("LLLL yyyy", date).toString().capitalize() // returns: Leden 2016

and

DateFormat.format("MMMM yyyy", date).toString() // returns ledna 2016

This solution is working for all API's.

Pang
  • 9,564
  • 146
  • 81
  • 122
Tom Wayne
  • 1,288
  • 1
  • 12
  • 31
1

tl;dr

Month.OCTOBER.getDisplayName ( 
        TextStyle.FULL_STANDALONE , 
        new Locale ( "cs", "CZ" ) 
) 

říjen

Thanks to the Answer by MikeX for showing this is a “standalone” language issue. Unlike English, some languages such as Czech alter the spelling of the month depending on the context of its usage.

Using java.time

The modern approach uses the java.time classes. The Question and other Answers are outdated, using the troublesome old date-time classes (Calendar, SimpleDateFormat, DateFormat, etc.) that are now legacy, supplanted entirely by the java.time classes. See below for back-port to Android.

The Month enum provides a method for localizing the name of the month: Month::getDisplayName. Pass a TextStyle indicating full-narrow-short and whether you want the standalone or combo type.

When can loop through these to see all the possible results.

Locale locale = new Locale ( "cs", "CZ" );
for ( Month month : EnumSet.allOf ( Month.class ) ) {
    System.out.println ( "\nMonth: " + month );
    for ( TextStyle textStyle : EnumSet.allOf ( TextStyle.class ) ) {
        String output = month.getDisplayName ( textStyle, locale ) + "  | " + textStyle;
        System.out.println ( output );
    }
}
Month: JANUARY
ledna  | FULL
leden  | FULL_STANDALONE
Led  | SHORT
I  | SHORT_STANDALONE
l  | NARROW
l  | NARROW_STANDALONE

Month: FEBRUARY
února  | FULL
únor  | FULL_STANDALONE
Úno  | SHORT
II  | SHORT_STANDALONE
ú  | NARROW
ú  | NARROW_STANDALONE

Month: MARCH
března  | FULL
březen  | FULL_STANDALONE
Bře  | SHORT
III  | SHORT_STANDALONE
b  | NARROW
b  | NARROW_STANDALONE

Month: APRIL
dubna  | FULL
duben  | FULL_STANDALONE
Dub  | SHORT
IV  | SHORT_STANDALONE
d  | NARROW
d  | NARROW_STANDALONE

Month: MAY
května  | FULL
květen  | FULL_STANDALONE
Kvě  | SHORT
V  | SHORT_STANDALONE
k  | NARROW
k  | NARROW_STANDALONE

Month: JUNE
června  | FULL
červen  | FULL_STANDALONE
Čer  | SHORT
VI  | SHORT_STANDALONE
č  | NARROW
č  | NARROW_STANDALONE

Month: JULY
července  | FULL
červenec  | FULL_STANDALONE
Čvc  | SHORT
VII  | SHORT_STANDALONE
č  | NARROW
č  | NARROW_STANDALONE

Month: AUGUST
srpna  | FULL
srpen  | FULL_STANDALONE
Srp  | SHORT
VIII  | SHORT_STANDALONE
s  | NARROW
s  | NARROW_STANDALONE

Month: SEPTEMBER
září  | FULL
září  | FULL_STANDALONE
Zář  | SHORT
IX  | SHORT_STANDALONE
z  | NARROW
z  | NARROW_STANDALONE

Month: OCTOBER
října  | FULL
říjen  | FULL_STANDALONE
Říj  | SHORT
X  | SHORT_STANDALONE
ř  | NARROW
ř  | NARROW_STANDALONE

Month: NOVEMBER
listopadu  | FULL
listopad  | FULL_STANDALONE
Lis  | SHORT
XI  | SHORT_STANDALONE
l  | NARROW
l  | NARROW_STANDALONE

Month: DECEMBER
prosince  | FULL
prosinec  | FULL_STANDALONE
Pro  | SHORT
XII  | SHORT_STANDALONE
p  | NARROW
p  | NARROW_STANDALONE

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154