我目前正在开发一个基本的 RSS Feed 程序,该程序在给定可解析的 Twitter 源的情况下显示推文。我目前正在做的是重新格式化日期。当我检索 pubDate 时,它以“EEE,d MMM yyyy HH:mm:ss Z”的形式解析。我想要做的是重新格式化它,这样当我在我的 GUI 上显示它时,它会显示为“MM/dd/yyyy HH:mm”。我该怎么做呢?这是必要的代码块:
try {
builder = factory.newDocumentBuilder();
Document feedDocument = builder.parse(sourceListItem);
XPathFactory xpfactory = XPathFactory.newInstance();
XPath xpath = xpfactory.newXPath();
String countStr = xpath.evaluate("count(/rss/channel/item)", feedDocument);
int itemCount = Integer.parseInt(countStr);
for(j=1; j<=itemCount; j++) {
try {
String title = xpath.evaluate("/rss/channel/item[" + j + "]/title", feedDocument);
String link = xpath.evaluate("/rss/channel/item[" + j + "]/link", feedDocument);
String date = xpath.evaluate("/rss/channel/item[" + j + "]/pubDate", feedDocument);
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Calendar c = Calendar.getInstance();
Date d = df.parse(date);
DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy HH:mm");
String dateFormat = df2.format(d);
c.setTime(d);
RSSItemClass rssItem = new RSSItemClass(title, link, c);
rssList.add(rssItem);
} catch (ParseException e) {
badSourceList.add(sourceListItem);
}
}
} catch (ParserConfigurationException e) {
badSourceList.add(sourceListItem);
}