2

I need to parse spanish dates with the following format (which are strings):

6 junio 2012 
20 mayo 2012 

junio means june and mayo means may.

As you can see the month is written in spanish. How can I normalize it to get normalized dates (yyyy-mm-dd)?

In the example above I my desired output should be:

2012-06-06
2012-05-20

To be honest I dont know how to even start, searching a solution of this specific case is a bit hard.

Thanks in advance

Avión
  • 7,963
  • 11
  • 64
  • 105
  • its a date variable or string? – CompEng Dec 01 '16 at 11:00
  • It is a string. – Avión Dec 01 '16 at 11:01
  • The marked as duplicated works with dates and I'm working with an input string. – Avión Dec 01 '16 at 11:05
  • @Avión: Indeed, but you can use the same `SimpleDateFormat` and use `parse(String)` instead of `format(Date)` , this will give you a correct date from a localized `String`. – Arnaud Dec 01 '16 at 11:06
  • Use java.time classes. `LocalDate.parse ( "6 junio 2012" , DateTimeFormatter.ofPattern ( "d MMMM uuuu" , new Locale ( "es" , "ES" ) ) ).toString ()` → `2012-06-06` (that is standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, BTW) – Basil Bourque Dec 02 '16 at 03:19

1 Answers1

5

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks. How can I parsed it to get the desired output format (`yyyy-mm-dd`)? – Avión Dec 01 '16 at 11:10
  • @Avión I have updated my answer per your requirements. – Tim Biegeleisen Dec 01 '16 at 11:20
  • Use another SimpleDateFormat: SimpleDateFormat dateFormatOutput = new SimpleDateFormat("yyyy-MM-dd", localeOutput); SimpleDateFormat dateFormatInput = new SimpleDateFormat("dd MMMM yyyy", localeInput); date = dateFormatInput.parse(inputdate); return dateFormatOutput.format(date); – Marco A. Hernandez Dec 01 '16 at 11:21
  • Thanks a lot, Tim. Just a little question. Why there are three `M`s in the month part on the `new SimpleDateFormat` part? `"dd MMM yyyy"`? It should be just two? – Avión Dec 01 '16 at 11:25
  • FYI, the troublesome classes of `java.util.Date` and `SimpleDateFormat` are now legacy, supplanted by the java.time classes. – Basil Bourque Dec 01 '16 at 20:44