Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Need to read cq:lastModified date and pass it to Solr document field; How to parse that date

Avatar

Level 3

Dear All,
 
I have a requirement where I need to read cq:lastModified property which is a date in the format -  "2015-06-17T15:43:16.352+05:30". Now I have read that date in a string format but when I try to add this date to SolrDocument field by parsing it using a SimpleDateFormat#parse, its giving an exception which says - Unparseable date: "2015-06-17T15:43:16.352+05:30".
 
Can someone please help me to parse this date so that I can add this to SolrDocument. SolrDocument date format is - "yyyy-MM-dd'T'HH:mm:ss'Z'"
 
Thanks,
Ravi

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi Ravi,

 

When adding data to Solr from CQ5:

First load property from the JCR using

final Date lastModified = valueMap.get(NameConstants.PN_PAGE_LAST_MOD, Date.class);

Now, we will need special helper method to transform Date to the UTC time format accepted by Solr

/** * Converts a date to the UTC DateField format that Solr understands. */ public static String convertToUtc(Date date) { final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:00.00'Z'"); formatter.setTimeZone(TimeZone.getTimeZone("UTC")); return formatter.format(date); }

Finally, you need to add your String to Solr:

solrInputDocument.addField("last-modified-page-prop", convertToUtc(lastModified);

 

@Ravi, what system/implementation are you using to add data to Solr from CQ5. Which system of CQ5 are using to add data to Solr?

 

Hope this helps,

Thanks,

Peter

View solution in original post

3 Replies

Avatar

Administrator

Hi Ravi KS 

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

 

Maybe this way you could feed "formattedDate" of type date to solr.

I hope this would help you.

 

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Correct answer by
Community Advisor

Hi Ravi,

 

When adding data to Solr from CQ5:

First load property from the JCR using

final Date lastModified = valueMap.get(NameConstants.PN_PAGE_LAST_MOD, Date.class);

Now, we will need special helper method to transform Date to the UTC time format accepted by Solr

/** * Converts a date to the UTC DateField format that Solr understands. */ public static String convertToUtc(Date date) { final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:00.00'Z'"); formatter.setTimeZone(TimeZone.getTimeZone("UTC")); return formatter.format(date); }

Finally, you need to add your String to Solr:

solrInputDocument.addField("last-modified-page-prop", convertToUtc(lastModified);

 

@Ravi, what system/implementation are you using to add data to Solr from CQ5. Which system of CQ5 are using to add data to Solr?

 

Hope this helps,

Thanks,

Peter

Avatar

Level 3

Hi All Thanks. Issue is resolved. I used - 

  1. final Date lastModified = valueMap.get(NameConstants.PN_PAGE_LAST_MOD, Date.class);

 

We are using CQ5.6.1 and Solr 4.10. Upon page activation, we were supposed to add the page to solr index. Hence I need this data.

 

Thanks,

Ravi