Unparseable date using DateFormat.parse()
Asked Answered
C

3

5

I followed a few other solutions on this site to this dilemma, and I do not have Joda Time installed, but I'm still at a loss as to why this is failing.

I also tried removing the colons, as one solution stated, but that did not help.

currentNode.getProperty("jcr:created").getString() = 2013-03-07T11:57:08.596-05:00

I get this error: java.text.ParseException: Unparseable date: "2013-03-07T11:57:08.596-05:00"

<%@page import="
    java.util.Date,
    java.text.SimpleDateFormat,
    java.text.DateFormat"
%>
<%
    DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy");
    DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    String currentDate = currentNode.getProperty("jcr:created").getString();
    Date date = inputFormat.parse(currentDate); // <-- Failing here
    String currentDateString = outputFormat.format(date);
%>
Carmencita answered 19/3, 2013 at 16:53 Comment(1)
K
13

The time zone formated as Z should be -0500, not -05:00.

So I'd suggest you to replace

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

with

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");

See SimpleDateFormat's javadoc for more details on available formats.

If your jdk doesn't allow the X pattern, you'll have to fix the input string to remove the :. This can be done with a regex :

currentDate = currentDate.replaceAll(":(\\d\\d)$", "$1")
Keelung answered 19/3, 2013 at 16:55 Comment(4)
java.lang.IllegalArgumentException: Illegal pattern character 'X'Carmencita
The X pattern is new for Java 7.Trenatrenail
I am looking at this solution after about 3 years of this answer and yet it was very helpful and there are no other solutions online like this. plus one for this good solutionLarcenous
What should I do if I'm converting type Date to ISO 8601 format in java 6? The solution proposed to remove ' : ' works in this case. But input will be Date type.Acquit
N
2

After testing the solution by drunken bot, I see that a timezone with half hours does not work, like -0530 (India).

So the improved answer therefore is:

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

Note the added XX at the end, now also minutes are taken into account.

Nonappearance answered 15/4, 2016 at 5:53 Comment(0)
M
0

Though this question has been answered, there is an alternative way to achieve the solution, if your requirement is only to display the date of creation in the format "MMMM dd,yyyy".

There is a getDate() method for Property object which returns a Calendar object, from which can get date object using getTime().

So, the above piece of code, would work if re-written as shown below.

<%@ page import="java.util.Calendar,
    java.text.SimpleDateFormat,
    java.text.DateFormat" %>
<%
DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy");
Calendar currentDate = currentNode.getProperty("jcr:created").getDate();
String currentDateString = outputFormat.format(currentDate.getTime()); %>

Thus, it would eliminate the need of converting the String to Date and then performing the remaining operations. Hope this helps.

Mafalda answered 20/3, 2013 at 4:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.