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);
Related
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.
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);
I use below code in Java and works perfect!
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
Date date = format.parse("Sun, 11 May 2014 23:11:51 +0430");
but in Android I got exception !
java.text.ParseException: Unparseable date: "Sun, 11 May 2014 23:11:51 +0430" (at offset 0)
what's wrong ?!
The problem is that the code will execute correctly if the default locale is english, otherwise will throw an exception. You can solve it adding the correct Locale.
//Locale locale = new Locale("en-US");
Locale locale = Locale.US;
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", locale);
Date date = format.parse("Sun, 11 May 2014 23:11:51 +0430");
Probably the android device has a different language setting. Consider using a constant Locale as RC stated in the comment, in that case you wouldn't need the extra variable, use the constant directly in the constructor.
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.US);
Date date = format.parse("Sun, 11 May 2014 23:11:51 +0430");
If the Locale on your device is German for example your code executes if you parse this date:
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.GERMAN);
Date date = format.parse("So, 11 Mai 2014 23:11:51 +0430");
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.
How can I parse a pubDate from a RSS feed to a Date object in java.
The format in the RSS feed:
Sat, 24 Apr 2010 14:01:00 GMT
What I have at the moment:
DateFormat dateFormat = DateFormat.getInstance();
Date pubDate = dateFormat.parse(item.getPubDate().getText());
But this code throws an ParseException with the message Unparseable date
You can define the date format you are trying to parse, using the class SimpleDateFormat:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
Date date = formatter.parse("Sat, 24 Apr 2010 14:01:00 GMT");
Additionally, for non-English Locale's, be sure to use the following when parsing dates in English:
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
If you need to have an RFC822 compliant date, try this :
DateFormat dateFormatterRssPubDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
For the lucky one that can use the Java 8 LocalDateTime:
LocalDateTime localDateTime = LocalDateTime.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse("Sat, 24 Apr 2010 14:01:00 GMT"));