Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

java - How to solve the error convert String date from xml file to an int format?

I also find myself facing the date problem today, I extracted the date from an xml file in string format but when I try to convert it into an int format I have an error.

This is a part of my code :

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("top");
for (int j=0; j<= inputFile.length();j++)
    for (int temp =0; temp < nList.getLength(); temp++) {
         j++;
         System.out.println("---------------------------------------");
         Node nNode = nList.item(temp);
         System.out.println("
 La requete numero " +j+ " " + nNode.getNodeName());
         if (nNode.getNodeType() == Node.ELEMENT_NODE) {
             Element eElement = (Element) nNode;
             dateq=eElement.getElementsByTagName("querytime").item(0).getTextContent();
             System.out.println("date de la requete est " +dateq);
             DateFormat dfq = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.FRENCH);
             Date date1 = dfq.parse(dateq);
             System.out.println("new date: " +date1);

and the output is :

date de la requete est  Tue Feb 08 12:30:27 +0000 2011 
java.text.ParseException: Unparseable date: " Tue Feb 08 12:30:27 +0000 2011 "
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As in your previous question I recommend you throw out the outdated classes SimpleDateFormat and friends. The modern classes are generally so much more programmer friendly.

Let’s try an experiment:

    String dateq = " Tue Feb 08 12:30:27 +0000 2011 ";
    DateTimeFormatter dtfFr = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
                                                          Locale.FRENCH);
    OffsetDateTime date1 = OffsetDateTime.parse(dateq, dtfFr);

Your exception message clearly shows there are spaces (or other invisible chars) in the beginning and end of your string, so I put them in my string too. Other than that your message just said that the date was unparseable, not why. In contrast, the above code gives java.time.format.DateTimeParseException: Text ' Tue Feb 08 12:30:27 +0000 2011 ' could not be parsed at index 0. Index 0, that is where the first space is, so it’s already a bit more helpful.

My suggestion for removing the spaces:

    dateq = dateq.trim();

This will work with all whitespace, not only space characters, and it will work if there are 0, 1, 2 or many of them. With this line inserted at the right place we get java.time.format.DateTimeParseException: Text 'Tue Feb 08 12:30:27 +0000 2011' could not be parsed at index 0. It’s almost the same message! This time we see no space in the beginning of the string, but it still says the problem is at index 0. This is now where it says “Tue”. The message again is to the point because the problem with “Tue” is it’s English, and you gave a locale of Locale.FRENCH. Having seen that, it’s not difficult:

    System.out.println("date de la requete est " + dateq);
    dateq = dateq.trim();
    DateTimeFormatter dtfEng = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
                                                           Locale.ENGLISH);
    OffsetDateTime date1 = OffsetDateTime.parse(dateq, dtfEng);
    System.out.println("new date: " + date1);

This prints:

date de la requete est  Tue Feb 08 12:30:27 +0000 2011 
new date: 2011-02-08T12:30:27Z

If you don’t know in advance whether you will have a date in English or French, just try both:

    dateq = dateq.trim();
    DateTimeFormatter dtfEng = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
                                                           Locale.ENGLISH);
    DateTimeFormatter dtfFr = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
                                                          Locale.FRENCH);
    OffsetDateTime date1;
    try {
        date1 = OffsetDateTime.parse(dateq, dtfEng);
    } catch (DateTimeParseException dtpe) {
        // didn’t work in English, try French
        date1 = OffsetDateTime.parse(dateq, dtfFr);
    }

This handles both dim. janv. 23 24:00:00 +0000 2011 and Tue Feb 08 12:30:27 +0000 2011.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...