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);
%>
justacoder
  • 2,684
  • 6
  • 47
  • 78

3 Answers3

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")
drunken bot
  • 486
  • 3
  • 8
  • java.lang.IllegalArgumentException: Illegal pattern character 'X' – justacoder Mar 19 '13 at 17:03
  • 6
    The `X` pattern is new for Java 7. – Perception Mar 19 '13 at 17:08
  • 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 solution – samir Nov 15 '16 at 07:46
  • 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. – Sunil Kumar Sep 01 '17 at 07:32
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.

Pavya
  • 6,015
  • 4
  • 29
  • 42
Mies44
  • 21
  • 1
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.

rakhi4110
  • 9,253
  • 2
  • 30
  • 49