How do I format the Spanish month in a sentence using simpledateformat?
•
Java
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,1))); } }
The above code returns,
01-ene-2014
But the month should be a sentence, ene
Anyone can help me how to get 01-ene-2014 without using substrings?
Solution
This is not a mistake
Simpledateformat uses month names and case according to local rules
In English months, initial capitalization, such as English grammar rules, is mandatory
Spanish is different The month name must be used as lowercase Java uses local rules These rules for Spanish are defined by, for example, Rae (Royal Spanish Academy)
In addition, you cannot create a custom locale with your own rules, but you can override your own month name with the dateformatsymbols class
DateFormatSymbols sym = DateFormatSymbols.getInstance(baseLocale); sym.setShortMonths(new String[]{"Ene","Feb","Mar",/* and others */ }); new SimpleDateFormat(aPattern,sym);
Complete example: http://ideone.com/R7uoW0
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码