【问题标题】:Format date in a specific format以特定格式格式化日期
【发布时间】:2012-12-04 06:39:56
【问题描述】:

我已经解析了一个 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();

}

这是它返回的内容:

如何正确格式化我的日期?

【问题讨论】:

  • 首先,更改您的代码示例以删除所有 XML 部分。它们与您的问题完全无关。

标签: java android parsing date formatting


【解决方案1】:

目前,您只是从 XML 节点中获取数据作为字符串。您需要以输入格式 parse 该字符串,然后以所需格式 format 它。这两个都可以使用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);

【讨论】:

    【解决方案2】:

    我希望您听说过 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的信息

    【讨论】:

      【解决方案3】:

      查看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]);
      
      ...
      

      【讨论】:

      • 请注意,您应该将SimpleDateFormat 的区域设置为Locale.US,否则它将在默认区域设置中查找月份和日期名称。
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-12-15
        • 1970-01-01
        • 2018-09-27
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多