-2

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("\n 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 "
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
celia
  • 29
  • 1
  • 7
  • I think you would look at the documentation: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html. Even if the date is 3 letters (Tue, Feb), in the specification one letter is enough. So it should be "E M dd HH:mm:ss z yyyy" – bracco23 Jun 04 '17 at 22:30
  • 4
    You already had plenty of explanations in your now deleted question (https://stackoverflow.com/questions/44351846/how-to-convert-xml-string-date-to-int-in-java): leading space, bad locale, obsolete DateFormat class but you ignored them all. You also had a question: which int value would you like to get? But you ignored it as well. Why should we care about this new question? – JB Nizet Jun 04 '17 at 22:31
  • @bracco23 I will try your proposal, Thank you. – celia Jun 04 '17 at 22:37

2 Answers2

1

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

The problem is in your lines

DateFormat dfq = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.FRENCH);
Date date1 = dfq.parse(dateq);

You got a ParseException because in " Tue Feb 08 12:30:27 +0000 2011 " you have leading and trailing space, and the parts Tue and Feb are English but not French.

Change these lines to

DateFormat dfq = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Date date1 = dfq.parse(dateq.trim());

and it will work.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
  • 2
    @celia Because the input text has a leading and trailing space. Look at the `ParseException` text. The spaces are clearly visible. – Andreas Jun 04 '17 at 22:51
  • Can you give me a little more explanation please. – celia Jun 04 '17 at 22:55
  • @celia What explanation besides the javadoc of [SimpleDateFormat](http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) do you need? – Thomas Fritsch Jun 04 '17 at 23:07
  • thank you, I hope I will solve this because I really need it – celia Jun 05 '17 at 07:50
  • 1
    While normally I would like to let the format take care of the string as it is, in this special situation I think I’d prefer to pass `dateq.trim()` to `parse()` to get rid of the leading and trailing spaces before parsing. This would work whether the spaces are there or not and whether there are 1, 2 or many spaces. – Ole V.V. Jun 05 '17 at 15:46
  • 1
    @OleV.V. I agree. Have merged your suggestion into my answer. – Thomas Fritsch Jun 05 '17 at 17:00