Parse RSS pubDate to Date object in java

梦想与她 提交于 2019-11-26 19:23:50

问题


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


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/2705548/parse-rss-pubdate-to-date-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!