SimpleDateFormat for month abbreviated in 3 characters

0

I am having problems when paging dates that express the abbreviated month to 3 characters. I use a SimpleDateFormar that I build in the following way:

DateFormat dateFormat = new SimpleDateFormat("dd MMM HH:mm yyyy", new Locale("es")) ;

When I try to parse a date with September month written "sep" I get an error because with this SimpleDateFormat September it represents it as "sept."

How can I get my SimpleDateFormat to be able to read the month of September represented by "sep"? I did several tests changing the Locale but I could not solve it.

    
asked by Jorge 20.08.2018 в 19:02
source

1 answer

1

You can use the DateFormatSymbols class in this way:

 DateFormatSymbols symbols;
 // la inicializas en idioma español, país México:
 symbols = new DateFormatSymbols( new Locale("es", "MX"));  // con MX es sep, con ES (España) es sept.
 DateFormat dateFormat = new SimpleDateFormat("dd MMM HH:mm yyyy", symbols);

You can create your own abbreviations even in uppercase with the setShortMonths method:

 // primero creas el String array
 String[] meses = {"ENE", "FEB","MAR", "ABR", "MAY", "JUN",
                   "JUL", "AGO","SEP", "OCT", "NOV", "DIC"};

 // lo configuras así, después de inicializar symbols:
 symbols.setShortMonths(meses);
    
answered by 21.08.2018 / 06:42
source