Need to read cq:lastModified date and pass it to Solr document field; How to parse that date | Community
Skip to main content
Level 3
October 16, 2015
Solved

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

  • October 16, 2015
  • 3 replies
  • 5981 views

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

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 Peter_Puzanovs

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

3 replies

kautuk_sahni
Community Manager
Community Manager
October 16, 2015

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
Peter_Puzanovs
Community Advisor
Peter_PuzanovsCommunity AdvisorAccepted solution
Community Advisor
October 16, 2015

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

Ravi_KSAuthor
Level 3
October 16, 2015

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