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

余生颓废 提交于 2019-12-21 10:56:07

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/27420236/how-to-format-the-spanish-month-in-sentence-case-using-simpledateformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!