"Error converting value to date", ValueFormat Exception thrown when trying to fetch date property from crx | Community
Skip to main content
New Participant
February 4, 2016
Solved

"Error converting value to date", ValueFormat Exception thrown when trying to fetch date property from crx

  • February 4, 2016
  • 10 replies
  • 4826 views

Hi All,

I am trying to fetch date property from crx as below :

Calendar cal = Node.getProperty("date").getValue().getDate();

"date" is of type "String" and has a value currently of "2014-02-01".

As per one of the article, http://aem.matelli.org/fetching-properties-from-dialogs/  this is one of the way to get the date value

1] Can you please guide me as to where I am missing things and what needs to be corrected.

Any thoughts/pointers on this will be helpful.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kunal_Gaba_

If the property is stored as string in JCR then why are you reading it as date in the JCR API-

Calendar cal = Calendar.getInstance(); String date = node.getProperty("date").getString() SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd"); Date dateObj = formatter.parse(date); cal.setTime(dateObj);

10 replies

askdctmAuthor
New Participant
February 8, 2016

Hi Jitendra/Kunal/Kautuk/Abhinav,

Thank you for your help here. Followed the thing mentioned above and it worked.

kautuk_sahni
Employee
February 8, 2016

Hi 

Please go through this Stackoverflow article.

Link:- http://stackoverflow.com/questions/22150895/save-date-as-timestamp-in-cq5

// JSP code

<%@page import="java.text.SimpleDateFormat,java.util.Date"%> <% SimpleDateFormat displayDateFormat = new SimpleDateFormat("dd MMM yyyy"); String dateField = properties.get("nameofdatefield", ""); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy"); Date formattedDate = sdf.parse(dateField); String formattedDateStr = displayDateFormat.format(formattedDate); out.println('Example of formated string'+formattedDateStr); %>

 

Reference Forum Thread :- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.html/forum__1yaj-dear_all_i_have.html

I hope this would help you.

Thanks and Regards

Kautuk Sahni

Kautuk Sahni
Kunal_Gaba_
Kunal_Gaba_Accepted solution
New Participant
February 5, 2016

If the property is stored as string in JCR then why are you reading it as date in the JCR API-

Calendar cal = Calendar.getInstance(); String date = node.getProperty("date").getString() SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd"); Date dateObj = formatter.parse(date); cal.setTime(dateObj);
Jitendra_S_Toma
New Participant
February 5, 2016

ValueFormatException, this exception occurs because "2014-02-01" isn't Date which JCR supports. Does it?. I guess, "date" property isn't Date type. So, you can't except value in date.

I would recommend you to read "date" property as String and convert it Date object. This is the simplest thing.

Jitendra

askdctmAuthor
New Participant
February 5, 2016

Hi All,

Any thoughts/pointers on the above will be helpful.

askdctmAuthor
New Participant
February 5, 2016

Hi Abhinav,

Thank you for your reply. 

https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/org/apache/jackrabbit/commons/JcrUtils.html lists JcrUtils.getDateProperty. 

Can you please let me know if it will work in the scenario mentioned.

New Participant
February 5, 2016

This would work only if you are using xtype as datetime. That would store it as a Date and then you can retrieve it like you are trying.

askdctmAuthor
New Participant
February 5, 2016

Hi All,

Any thoughts/pointers on this will be helpful.

askdctmAuthor
New Participant
February 4, 2016

Hi Jitendra,

Thank you for your reply.

As of now, it is not returning anything. It is throwing ValueFormatException.

As oer the  documentation, looks like it should return Calendar object.

Jitendra_S_Toma
New Participant
February 4, 2016

 Does this code return Date object or calender object?

Node.getProperty("date").getValue().getDate();

askdctm wrote...

Hi All,

I am trying to fetch date property from crx as below :

Calendar cal = Node.getProperty("date").getValue().getDate();

"date" is of type "String" and has a value currently of "2014-02-01".

As per one of the article, http://aem.matelli.org/fetching-properties-from-dialogs/  this is one of the way to get the date value

1] Can you please guide me as to where I am missing things and what needs to be corrected.

Any thoughts/pointers on this will be helpful.