Search code examples
javalocale

Locales and dates with SimpleDateFormat


I have a little question about how I could work with different Locales and dates.

I am from Spain, so my Locale is es_ES, sometimes I have to parse dates that it were wrote with others Locales and I don't know at first glance which Locale it's going to have the date. So, If I don't set the proper Locale it won't work, even, I don't want to be changing the Locale all time. I know that SimpleDateFormat has a parameter where you can give the Locale, but, even sometimes, I don't know what Locale is.

How could I parse any date independently of the Locale. Is it possible??


Solution

  • You can iterate over several Locales and try to parse with each locale. Return the first successful one. If you catch exception - check next Locale.

    The Locales should be ordered by likelihood of using.

    This is not perfect, but if you can't get the locale from the user, I think it is the best solution.