简体   繁体   中英

Format date in a specific format

I have parsed an RSS feed and I am looking for how to format the date properly. I am trying to get it to say something like, Wednesday, December 4, 2012. I am running it through a for loop to generate my data. Here's the code that I'm using:

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();

}

This is what it returns:

Android日期发行

How do I format my date properly?

Currently you're just getting the data out of the XML nodes as a string. You need to parse that string in the input format, then format it in your desired format. Both of these can be achieved using SimpleDateFormat .

You need to work out which time zone you want use for the output - the input already has "offset from UTC" so you don't need to worry about that.

Next you need to work out how you want to handle internationalization. Presumably you want to display the information in the user's locale? If so, you may well want to use DateFormat.getDateInstance() instead of SimpleDateFormat . Don't forget to use Locale.US for the input format though, given that that doesn't depend on the user.

So as a complete example, you might want:

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);

I hope you heard about SimpleDateFormat to format date, now you are getting date from feed as E, dd MMM yyyy HH:mm:ss Z So have to format it as EEE, MMMM d, yyyy . to do that try the following..

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();
                }

you can get info about DateFormat here

see a 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]);

...

If you are looking for printing/parsing the full name of week day and the month name, the pattern is as follows:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM