开发安卓应用程序,需要从RSS解析pubDate标签。从这个标签中显示实际时间没有问题,但是它太长了
<pubDate>Wed, 15 Nov 2017 14:46:40 +0000</pubDate>
我真正感兴趣的是date是如何被提取出来的,在本例中是15 Nov。此外,我想比较日期和显示降序的基础上的pubDate标签的帖子。
发布于 2017-12-29 16:31:45
如果你使用Rome来阅读RSS,你可以像下面这样格式化pubDate。https://rometools.github.io/rome/
getPubDate()返回日期类型。您可以按流操作进行排序或比较。
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;
public class NewsService {
public List<String> getNews(String url) throws Exception {
// read RSS
SyndFeed feed = new SyndFeedInput().build(new XmlReader(new URL(url)));
// format pubDate and create pubDate string list
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM");
return feed.getEntries().stream().map(i -> i.getPublishedDate()).map(sdf::format).collect(Collectors.toList());
}
}发布于 2018-01-08 07:38:42
Java类库可以解析RSS日期/时间值。
RSS日期/时间值采用RFC 822 format格式。您可以使用java.text包中的DateFormat和SimpleDateFormat类来解析其中一个日期。
首先,为RFC822创建一个日期格式化程序,并使用它来解析pubdate元素中的字符串:
DateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Date pubdate = formatter.parse("Wed, 15 Nov 2017 14:46:40 +0000");对parse()的调用会抛出java.text包中的ParseException,因此必须用try/catch将其括起来,或者在方法中使用throws子句。
接下来,创建日历并将其设置为该日期:
Calendar cal = Calendar.getInstance();
cal.setTime(pubdate);最后,从日历中获取月份和日期。
System.out.println("Month: " + cal.get(Calendar.MONTH));
System.out.println("Day: " + cal.get(Calendar.DAY_OF_MONTH));https://stackoverflow.com/questions/47961570
复制相似问题