Reputation: 507
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
Upvotes: 4
Views: 2206
Reputation: 1378
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.
Upvotes: 4
Reputation: 338730
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.
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
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?
Upvotes: 1
Reputation: 51
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));
}
Upvotes: 5
Reputation: 44439
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
).
Upvotes: 3