0

I'm trying to parse a String value in the format Sat Dec 03 20:30:33 GMT+00:00 2016 to a Date variable, but I'm getting an java.text.ParseException: Unparseable date: "Sat Dec 03 20:30:33 GMT+00:00 2016" (at offset 0) exception. What am I doing wrong here?

Date itemDate = new Date();
        DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss");
        try {
            itemDate = df.parse(c.getString(c.getColumnIndexOrThrow(ArticlesContract.ArticleEntry.COLUMN_NAME_DATE)));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
KaiZ
  • 391
  • 1
  • 5
  • 13
  • 1
    First of all, what is your current locale? Second, where are you parsing the end of the String, which is `GMT+00:00 2016`? – Tunaki Dec 03 '16 at 21:20
  • @Tunaki To be honest I'm not sure, but I think it is pt_PT for the locale. As for the end of the String, is that relevant to this error? I wasn't sure of how to parse that part, so I left it for later. – KaiZ Dec 03 '16 at 21:23
  • No that second part will be your next error. The current error is that `Sat` cannot be understood as a portuguese day of week. See also http://stackoverflow.com/a/23790945/1743880, you need to change the locale used by the formatter. – Tunaki Dec 03 '16 at 21:26

2 Answers2

1

this works

    private static void test1() {
            Date itemDate = new Date();
            DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss", Locale.ENGLISH);
            try {
                itemDate = df.parse("Sat Dec 03 20:30:33");
//"Sat Dec 03 20:30:33 GMT+00:00 2016" works too.
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(itemDate);
        }

the problem is about 'Dec' month. You have got another locale in your device

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

Sat Dec 03 20:30:33 GMT+00:00 2016

is a string with some locale dependent elements like "Sat" and "Dec"

therefore you have to do:

DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss", Locale.ENGLISH);

then be sure the pattern you are using is matching the string you want to parse...

in your example we see this : "EEE MMM dd HH:mm:ss" but the string to parse has for example the year to... so you need to parse that info as well adding in the correct place the year component yyyy

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97