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
Solved! Go to Solution.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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
Views
Replies
Total Likes
Hi All Thanks. Issue is resolved. I used -
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
Views
Replies
Total Likes
Views
Likes
Replies
Views
Like
Replies