1

I need to parse a String to Date in Java. The problematic scenario is when the full date pattern is used and it depends on a specific locale (that I don't know previously)

For example, using pattern "EEEE dd MMMM yyyy" two possible inputs are:

English = "Friday 10 November 2014"

Spanish = "Viernes 10 Noviembre 2014"

Is it possible to convert the above inputs to a Date object without to know the source locale?

Thanks.

castagu
  • 11
  • 1
  • 1
    I don't think this is possible with `SimpleDateFormat` alone. – Drux Nov 23 '14 at 21:10
  • 2
    You'd need to provide a `SimpleDateFormat` for each `Locale` you are likely to run into and then you would need simply loop through them until one works... – MadProgrammer Nov 23 '14 at 21:33
  • possible duplicate of [How to parse dates in multiple formats using SimpleDateFormat](http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat) – Basil Bourque Nov 24 '14 at 00:06

1 Answers1

0

I try the MadProgrammer solution and it works:

        String dateInString = "Friday 10 November 2014";
        //String dateInString = "Viernes 10 Noviembre 2014";
        Locale localeList[] = DateFormat.getAvailableLocales();

        Date date = null;

        for (Locale l : localeList) {
            try {

                SimpleDateFormat formatter = new SimpleDateFormat("EEEE dd MMMM yyyy", l);

                date = formatter.parse(dateInString);
                System.out.println("Trying with locale: " + l.toString());
                break;
            } catch (ParseException e) {
            }

        }

Thanks

castagu
  • 11
  • 1