将 RSS pubDate 解析为 Java 中的 Date 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2705548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:04:12  来源:igfitidea点击:

Parse RSS pubDate to Date object in java

javadaterssdate-format

提问by Derk

How can I parse a pubDate from a RSS feed to a Date object in java.

如何将 pubDate 从 RSS 提要解析为 java 中的 Date 对象。

The format in the RSS feed: Sat, 24 Apr 2010 14:01:00 GMT

RSS 提要中的格式:2010 年 4 月 24 日星期六 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

但是这段代码会抛出一个 ParseException 和消息 Unparseable date

采纳答案by Jo?o Silva

You can define the date format you are trying to parse, using the class SimpleDateFormat:

您可以使用类定义您尝试解析的日期格式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:

此外,对于非英语Locale的,在解析英语日期时一定要使用以下内容:

new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);

回答by mirage59

If you need to have an RFC822 compliant date, try this :

如果您需要符合 RFC822 的日期,请尝试以下操作:

DateFormat dateFormatterRssPubDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);

回答by Bruno Eberhard

For the lucky one that can use the Java 8 LocalDateTime:

对于可以使用 Java 8 LocalDateTime 的幸运儿:

LocalDateTime localDateTime = LocalDateTime.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse("Sat, 24 Apr 2010 14:01:00 GMT"));