How to format the Spanish month in sentence case using SimpleDateFormat?

This is my code :

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.SimpleDateFormat;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        SimpleDateFormat date = new SimpleDateFormat("dd-MMM-yyyy", new Locale("es","ar"));
        System.out.println(date.format(new Date(2014-1900,0,1)));
    }
}

The above code returns,

01-ene-2014

But, the month should be in sentence case, i.e Ene

Can anyone help me out how can I get 01-Ene-2014 without using substring?

Answer

This is not a bug.

SimpleDateFormat uses the month names and capitalization according to the local rules.

In english months appers with first letter capitalized as in the English grammar rules this is mandatory.

In spanish is not the same. Is mandatory to use the month names as lowercase. Java uses the local rules. These rules for spanish are defined for example by the RAE(Royal academy of spanish language)

Also there is no way to create a custom Locale with your own rules but you can use the class DateFormatSymbols to override the Month names with your own.

DateFormatSymbols sym = DateFormatSymbols.getInstance(baseLocale);
sym.setShortMonths(new String[]{"Ene","Feb","Mar", /* and others */ });
new SimpleDateFormat(aPattern, sym);

Full example: http://ideone.com/R7uoW0

Leave a Reply

Your email address will not be published. Required fields are marked *