Expand my Community achievements bar.

SOLVED

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

Avatar

Level 9

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.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

10 Replies

Avatar

Level 9

 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.

 

Avatar

Level 9

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.

Avatar

Level 9

Hi All,

Any thoughts/pointers on this will be helpful.

Avatar

Level 2

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.

Avatar

Level 9

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.

Avatar

Level 9

Hi All,

Any thoughts/pointers on the above will be helpful.

Avatar

Level 9

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

Avatar

Correct answer by
Employee Advisor

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

Avatar

Administrator

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-manage...

I hope this would help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 9

Hi Jitendra/Kunal/Kautuk/Abhinav,

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