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

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

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.
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);

10 replies

askdctmAuthor
Level 9
February 8, 2016

Hi Jitendra/Kunal/Kautuk/Abhinav,

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

Translate
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

Translate
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);
Translate
Jitendra_S_Toma
Level 10
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

Translate
askdctmAuthor
Level 9
February 5, 2016

Hi All,

Any thoughts/pointers on the above will be helpful.

Translate
askdctmAuthor
Level 9
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.

Translate
Level 2
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.

Translate
askdctmAuthor
Level 9
February 5, 2016

Hi All,

Any thoughts/pointers on this will be helpful.

Translate
askdctmAuthor
Level 9
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.

Translate
Jitendra_S_Toma
Level 10
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.

 

Translate