Unparseable date error - java

I have this
java.text.ParseException: Unparseable date: "Thu, 21 Apr 2016 18:00:00
+0000" (at offset 26)
when using new SimpleDateFormat("E, dd MMMM yyyy hh:mm:ss a", Locale.ROOT);
Why is that can be?
EDIT:
This is correct answer due to parsing pattern.
SimpleDateFormat f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ROOT);
And also Android somehow gives error when the Locale is ROOT and when it is ENGLISH everything works fine.

You are using the wrong format. You should be using EEE, dd MMM yyyy HH:mm:ss Z instead of E, dd MMMM yyyy hh:mm:ss a.
Here is the code snippet:
public static void main (String[] args) throws Exception
{
String foo = "Thu, 21 Apr 2016 18:00:00 +0000";
SimpleDateFormat f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ROOT);
System.out.println(f.parse(foo));
}
Output:
Thu Apr 21 18:00:00 GMT 2016

Use EEE, dd MMM yyyy HH:mm:ss Z

Related

SimpleDateFormat throws parse Exception for +0100

I am trying with two sets of date with date format :
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
It works fine for the Date : Fri, 26 Aug 2016 13:55:34 +0000
Not for the Date : Tue, 06 Sep 2016 11:57:14 +0100
Throws exception for the +0100 date.
Unparseable date: "Tue, 06 Sep 2016 11:57:14 +0100" (at offset 0)
at java.text.DateFormat.parse(DateFormat.java:555)
It fails at offset 0, which means that the problem is not related to the timezone but to the day in letters.
You should set the Locale of your SimpleDateFormat.
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
Date d1 = format.parse("Fri, 26 Aug 2016 13:55:34 +0000");
Date d2 = format.parse("Tue, 06 Sep 2016 11:57:14 +0100");
Works without any problem.
If you also need to retrieve the timezone, you will also have to add z to your pattern:
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
You need
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Note the z for the time zone.
The parser ignores the zero (+0000) case if z is not supplied, but not a non-zero (+0100) case. The lenient property controls this behaviour (Acknowledge #Marko Topolnik).
Since you're using English week names, you ought to use the two-argument constructor to SimpleDateFormat, passing Locale.ENGLISH as the second parameter.

Joda DateTimeFormatter Invalid format

I am getting the above error, but to me everything seems to be correct.
What I am doing wrong?
DateTimeFormatter simpleDateFormatInput= DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
DateTime datetime = simpleDateFormatInput.parseDateTime(pubDate);
Where pubDate is Sat, 30 Jan 2016 12:23:53 +0100
The day and/or month from your input String may not match those from your default Locale. Try
DateTimeFormatter simpleDateFormatInput =
DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z").withLocale(Locale.US);

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,
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);

Parse time with SimapleDateFormat

I'm trying to parse date like this: Tue Aug 28 21:16:23 +0000 2012 with this code:
SimpleDateFormat format = new SimpleDateFormat("E M dd HH:mm:ssZ yyyy", Locale.ENGLISH);
String d = object.getString("created_at"); // d = Tue Aug 28 21:16:23 +0000 2012;
date = format.parse(d);
But there is exception:
09-28 11:10:24.471: W/System.err(10388): java.text.ParseException: Unparseable date: "Fri Sep 28 07:09:09 +0000 2012" (at offset 4)
Where I make a mistake?
try this
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
MMM is used to represent short Month.
you need MMM for month representation for Aug.
SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ssZ yyyy", Locale.ENGLISH);
String d = "Tue Aug 28 21:16:23 +0000 2012"; // d =;
Date date = format.parse(d);
System.out.println(date);
Output:Tue Aug 28 22:16:23 BST 2012
It might help to look into SimpleDateFormat's javadoc, there are some helpful examples for pattern strings.

How to handle the Time data from the Style just like "Wed Jun 20 11:01:05 +0800 2012"?

In my project ,I get json data from Server,there's a field named 'creat_at' in the json which style is like 'Wed Jun 20 11:01:05 +0800 2012'
How to change it to more easy-to-read style like '2012/06/20'?
(I have tried 'DateFormat.parse' but it dose not work:
new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy").parse(dateString)
cause
java.text.ParseException: Unparseable date: "Wed Jun 20 11:00:53 +0800 2012")
See SimpleDateFormat API
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy").parse(dateString);
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
You need to try SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(sdf2.format(sdf.parse("Wed Jun 20 11:01:05 +0800 2012")));
prints
2012/06/20
You may also need to set an approriate timezone.

Categories