TootsieRockNRoll
TootsieRockNRoll

Reputation: 3288

Android can't parse string to date

I have this string

Wed, 08 Jan 2014 9:30 am WET

and needed to be parsed to a Date object, I tried lot of masks but didn't work, here is the last thing I tried that I thought it would work with but didn't

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aaa z", Locale.ENGLISH);

thanks

stack trace

01-08 14:25:25.906: W/System.err(13288): java.text.ParseException: Unparseable date: "Wed, 08 Jan 2014 11:59 am WET"
01-08 14:25:25.914: W/System.err(13288):    at java.text.DateFormat.parse(DateFormat.java:626)

I ended up using this instead

SimpleDateFormat dateFormat     = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aaa", Locale.ENGLISH);
Date date = dateFormat.parse(dateString.substring(0, dateString.length() - 4));

that WET part was the cause so I removed it, it wouldn't give the exact time but I only need the day and month,

Upvotes: 4

Views: 946

Answers (1)

Alexis C.
Alexis C.

Reputation: 93882

Give a Locale to your Formatter where days and months are in English, otherwise it will use your default locale (that I presume is not English) and hence can't parse your String.

SimpleDateFormat dateFormat =
      new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aaa z", Locale.ENGLISH);

Upvotes: 5

Related Questions