SimpleDateFormat throws parse Exception for +0100 - java

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.

Related

Parse date with AEDT and AEST time zone in java

I am trying to parse a string of format
Thu Apr 07 11:45:28 AEST 2016
into date object. My code looks like following:
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
try{
Date time = parserSDF.parse("Sat Feb 01 15:00:19 AEDT 2014");
}catch(Exception e){
e.printStackTrace();
}
But I am getting a 'parse error'. I cannot change the input format of the date and I also cannot set my timezone to a static value as this code is to be run on andorid device. How can I parse this string into date ?
Using the java.time framework (JSR 310), you can do:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss zzz yyyy");
ZonedDateTime zdt = ZonedDateTime.parse("Sat Feb 01 15:00:19 AEDT 2014", dtf);
System.out.println(zdt);
…which prints:
2014-02-01T15:00:19+11:00[Australia/Sydney]
Though why it picks Sydney instead of Melbourne I am not sure.

String to Date parse java

I want to parse a string Thu Apr 03 07:53:53 BST 2014 to a Date object in java. I don't know how to handle the 'BST' part - i have tried this
date = new SimpleDateFormat("E MMM dd HH:mm:ss yyyy", Locale.ENGLISH).parse(string);
but getting Unparseable date: "Thu Apr 03 07:53:53 BST 2014"
You're missing the BST part. Use zzz for it:
date = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(string);
That is because the date does not match the format string. The date has a timezone but the format string does not.
Thu Apr 03 07:53:53 BST 2014
E MMM dd HH:mm:ss ??? yyyy
Adding timezone (z) to the format string solves your problem:
date = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
.parse(string);
You could use the Joda Time library.
Try this one along with your locale using SimpleDateFormat.
// Thu Apr 03 07:53:53 BST 2014
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date date = format.parse("Thu Apr 03 07:53:53 BST 2014");
Description of each pattern
EEE - Day name in week (in 3 chars)
MMM - Month in year (in 3 chars)
dd - Day in month (in 2 digits)
kk - Hour in day (1-24) (in 2 digits)
mm - Minute in hour (in 2 digits)
ss - Second in minute (in 2 digits)
z - Time zone (General time zone)
yyyy - Year (in 4 digits)
date = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
.parse(string)
Your time format doesn't match with the string. You missed the z in your format where it refers to the timezone. Check out this link for more detailsSimpleDateFormat

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