2012-12-04 170 views
0

我已解析RSS提要,并且正在寻找如何正确格式化日期。我试图说出类似于2012年12月4日星期三的内容。我通过for循环来运行它以生成我的数据。下面是我使用的代码:以特定格式格式化日期

Feed = new URL(URLFeed); 
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance(); 
db = dbf.newDocumentBuilder(); 
doc = db.parse(new InputSource(Feed.openStream())); 
doc.getDocumentElement().normalize(); 
nodeList = doc.getElementsByTagName("item"); 

title = new String[nodeList.getLength()]; 
pubDate = new String[nodeList.getLength()]; 
link = new String[nodeList.getLength()]; 

for(int i=0;i<nodeList.getLength();i++){ 
    Node node = nodeList.item(i); 

    Element fstElmnt = (Element) node; 

    NodeList titleList = fstElmnt.getElementsByTagName("title"); 
    Element titleElement = (Element) titleList.item(0); 
    titleList = titleElement.getChildNodes(); 
    title[i] = ((Node) titleList.item(0)).getNodeValue(); 


    NodeList pubDateList = fstElmnt.getElementsByTagName("pubDate"); 
    Element pubDateElement = (Element) pubDateList.item(0); 
    pubDateList = pubDateElement.getChildNodes(); 
    pubDate[i] = ((Node) pubDateList.item(0)).getNodeValue(); 


    NodeList linkList = fstElmnt.getElementsByTagName("link"); 
    Element linkElement = (Element) linkList.item(0); 
    linkList = linkElement.getChildNodes(); 
    link[i] = ((Node) linkList.item(0)).getNodeValue(); 

} 

这是它返回:

Android Date Issue

如何正确格式化我约会吗?

+0

首先,更改您的代码示例以摆脱所有XML部件。他们*完全*与您的问题无关。 –

回答

4

当前,您只是将数据作为字符串从XML节点中取出。你需要解析表示输入格式中的字符串,然后格式它以你想要的格式。这两个都可以使用SimpleDateFormat来实现。

您需要计算出您希望用于输出的时区 - 输入已经具有“与UTC的偏移”,因此您不必担心这一点。

接下来你需要弄清楚你想如何处理国际化。想必你想在用户的语言环境中显示信息?如果是这样,您可能需要使用DateFormat.getDateInstance()而不是SimpleDateFormat。不要忘记使用Locale.US作为输入格式,但考虑到并不是取决于用户。

所以,作为一个完整的例子,你可能会想:

SimpleDateFormat inputFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", 
                Locale.US); 
DateFormat outputFormat = DateFormat.getDateInstance(DateFormat.LONG, 
                userLocale); 
// TODO: Set time zone in outputFormat 

Date date = inputFormat.parse(inputText); 
String outputText = outputFormat.format(date); 
0

看到Parse RSS pubDate to Date object in java

pubDate[i] = ((Node) pubDateList.item(0)).getNodeValue(); 
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); 
Date date = formatter.parse(pubDate[i]); 

... 
+0

请注意,您应该将'SimpleDateFormat'的语言环境设置为'Locale.US',否则它将在默认语言环境中查找月份和日期名称。 –

1

我希望你听说过SimpleDateFormat格式化日期,现在你从进料越来越日期E,dd MMM yyyy HH:mm:ss Z所以必须格式化为EEE,MMMM d,yyyy。要做到这一点尝试以下..

SimpleDateFormat fromFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.US); 
SimpleDateFormat toFormat = new SimpleDateFormat("EEE, MMMM d, yyyy", Locale.US); 

try { 
     Date fromDate = fromFormat.parse(pubDate[i]) //pubDate[i] is your date (node value) 
     pubDate[i] = toFormat.format(fromDate); 
    } catch (Exception e) { 
        e.printStackTrace(); 
       } 

你可以得到信息关于DateFormat here

0

如果您正在寻找印刷/解析星期几的全名和月份名称,模式如下: -

Format formatter = new SimpleDateFormat("EEEE,MMMM,dd"); 
String s = formatter.format(new Date()); 
System.out.println(s);