Try the following code. The trick here is to initialize the SimpleDateFormat
with both a language and country, in this case Spanish (Español) and Spain (España).
String yourDate = "20 mayo 2012";
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy", new Locale("es","ES"));
Date date = sdf.parse(yourDate);
System.out.println(date.toString());
Output:
Sun May 20 00:00:00 SGT 2012
If you want to format this Spanish date as yyyy-mm-dd
you can again use the format()
method from SimpleDateFormat
:
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-mm-dd");
System.out.println(sdf2.format(date));
Update:
The reason we use MMM
in the format mask for the full month name is explained well in the Javadoc for SimpleDateFormat:
Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
I have seen both MMM
and even MMMM
being used to what seems to be the same effect.