Parse RSS pubDate to Date object in java
Asked Answered
S

3

39

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

Spoil answered 24/4, 2010 at 18:0 Comment(0)
C
68

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);
Culver answered 24/4, 2010 at 18:18 Comment(0)
W
14

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);
Wayland answered 28/10, 2011 at 9:8 Comment(0)
R
7

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"));
Rb answered 9/8, 2016 at 7:3 Comment(3)
That’s not only easier, it’s first and foremost less errorprone. Thanks. PS LocalDateTime and DateTimeFormatter have been backported to Java 6 and 7 in ThreeTen Backport.Paulinepauling
Exception when: 'Tue, 12 Mar 2019 10:05:52 EDT' and Text 'Wed, 6 Mar 2019 14:05:00 PST' could not be parsed at index 25.Spalla
@DanielDeLeón That seems to agree with the documentation: North American zone names and military zone names are not handled. I gather that RSS follows RFC 822 here, and that PST, PDT, MST, MDT, CST, CDT, EST and EDT are allowed: link.Paulinepauling

© 2022 - 2024 — McMap. All rights reserved.