格式化日期时RSS阅读器错误

时间:2012-04-03 16:28:23

标签: android rss date-format

所以我有一个很棒的RSS阅读器源代码,它工作完美,但我有一个问题,我需要将日期从星期五,2012年3月30日05:09:20 +0000转换为简单格式“yyyy / MM / dd hh:mm:ss“,但我无法使其工作,因为两种数据类型冲突,NodeList和Date。

public class rssparser {


private static NodeList newdate;

private static NodeList formmatter;
private static NodeList formatter;
private static Intent event;
private static ResourceBundle bundle;
private static NodeList pubdate1;


public static void parse(){
URL url;
try {
    url = new URL("http://www.gaudeamus.fm/feed/");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc;
          doc = db.parse(url.openStream());
          doc.getDocumentElement().normalize();
          NodeList itemLst = doc.getElementsByTagName("item");



          arrays.PodcastTitle = new String[itemLst.getLength()];
          arrays.PodcastURL = new String[itemLst.getLength()];
          arrays.PodcastContent = new String[itemLst.getLength()];
          arrays.PodcastMedia = new String[itemLst.getLength()];
          arrays.PodcastPubDate = new String[itemLst.getLength()];

       // SimpleDateFormat pubdate = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
       // Date formmater = formatter.parse("Sat, 24 Apr 2010 14:01:00 GMT");


          for(int i=0; i < itemLst.getLength(); i++){

                Node item = itemLst.item(i);
                if(item.getNodeType() == Node.ELEMENT_NODE) {
                    Element ielem = (Element) item;
                    NodeList title = ielem.getElementsByTagName("title");
                    NodeList link = ielem.getElementsByTagName("link");
                    NodeList pubdate = ielem.getElementsByTagName("pubDate");
                    //NodeList description = ielem.getElementsByTagName("description");
                    NodeList content = ielem.getElementsByTagName("content:encoded");


                     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                     String formatedDate =sdf.parse(pubdate);


                    arrays.PodcastTitle[i] = title.item(0).getChildNodes().item(0).getNodeValue();
                        arrays.PodcastTitle[i] +=formatedDate+"  \n" + pubdate.item(0).getChildNodes().item(0).getNodeValue();
                        arrays.PodcastURL[i] = link.item(0).getChildNodes().item(0).getNodeValue();
                        arrays.PodcastContent[i] = content.item(0).getChildNodes().item(0).getNodeValue();
                        arrays.PodcastPubDate[i] = pubdate.item(0).getChildNodes().item(0).getNodeValue();
                        //arrays.PodcastMedia[i] = mediaurl;




                }

          }

    }

} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (DOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}


}

这是一个代码,

String formatedDate =sdf.parse(pubdate);但我的问题是解析需要String类型,但我有NodeList,我尝试了很多变种,但它不起作用。如果我不进行解析但格式类似于String formatedDate =sdf.format(pubdate);它没有显示任何错误,但是当我启动我的应用程序时,它会在加载新闻时崩溃。

有人能帮助我吗?抱歉英语不好。

3 个答案:

答案 0 :(得分:0)

解析这个:

arrays.PodcastPubDate[i] = pubdate.item(0).getChildNodes().item(0).getNodeValue(); 

而不是NodeList?我假设那是String;你不能只解析一个NodeList,它没有任何逻辑意义。

答案 1 :(得分:0)

你不仅有XML解析问题。你也有时间格式解析问题

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss Z");
Date formatedDate = sdf.parse(((Node)((Element)pubdate.item(0)).getChildNodes().item(0)).getNodeValue());

答案 2 :(得分:0)

尝试指定美国格式:

public void setPubDate(String pubDate) {
    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US);
        this.pubDate = dateFormat.parse(pubDate);

    } catch (java.text.ParseException e) {
        Log.e("MySoft", title);
    }
}