开发Android应用程序,需要从RSS中解析pubDate标签。显示此标签的实际时间没有问题,但太长了
<pubDate>Wed, 15 Nov 2017 14:46:40 +0000</pubDate>
我真正感兴趣的是如何约会,在本例中为 15 Nov
被提取。此外,我想比较日期并根据 pubDate 标签按降序显示帖子。
最佳答案
如果你使用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());
}
}
关于java - 从 pubDate RSS 标签中提取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47961570/