AEM 6.5 Sitemap lastmod date formatting | Community
Skip to main content
Level 2
November 23, 2023
Solved

AEM 6.5 Sitemap lastmod date formatting

  • November 23, 2023
  • 3 replies
  • 3118 views

Hi All,

 

Hope you can help me clarify below requirement in my current project that is using AEM 6.5 related to Sitemap. One of my teammates implemented this approach:

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/generate-sitemap-xml-in-6-5-10/m-p/541339

 

The requirement from business is to have "lastmod" value in yyy-MM-dd format. I wonder if that could be possible since the Url.setLastModified method accepts as a parameter an Instance object that is already defined in the interface class from URL.class. Using Calendar.toInstance() method will output always YYYY-MM-DDTHH:mm:ss. I'm not sure if there might be another alternative for the requirement. Appreciate your inputs whether there is a possible solution or not so that I can clarify it and express the limitations to Business.

Thank you and best regards.

 

 

 

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 A_H_M_Imrul

@luis_ochoa 

An Instant in Java represents an instantaneous point in time on the timeline. It's stored in UTC and typically represented in the format of "yyyy-MM-ddTHH:mm:ss.SSSZ" where "T" separates the date and time, "Z" signifies UTC, and the time includes hours, minutes, seconds, and milliseconds. However, you can convert an Instant to a LocalDate (which represents just a date without time or timezone information) and then back to an Instant if needed.

 

Instant instant = Instant.now(); // Replace this with your specific Instant // Convert Instant to LocalDate LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate(); // Convert LocalDate back to Instant (with only date and no time) Instant dateOnlyInstant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); System.out.println(dateOnlyInstant); // Output: 2023-11-26T00:00:00Z

 

Thats the maximum you can achieve with this approach.  Otherwise, you can check this customizable solution: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/adobe/acs/commons/wcm/impl/SiteMapServlet.java 

3 replies

A_H_M_Imrul
Community Advisor
Community Advisor
November 23, 2023

@luis_ochoa 

Try this:

 

if (lastmod != null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = sdf.format(lastmod.getTime()); System.out.println("Formatted Date: " + formattedDate); // Use `formattedDate` as needed in your code Instant instant = null; try { instant = sdf.parse(formattedDate).toInstant(); // you can then set instant whereever you want } catch (java.text.ParseException e) { e.printStackTrace(); // Handle parsing exception if needed } } else { // Handle case where `lastmod` is null System.out.println("Last Modified Date is null"); }

 

Level 2
November 23, 2023

Hi silk_route11, 

thanks for your help, I've rested your code in my local environment. It works fine except that it still shows the date inYYYY-MM-DDTHH:mm:ss  when parsing the formattedDate to instance. Kindly see below screenshot.


Is there any other alternative I can follow?.

regards

 

A_H_M_Imrul
Community Advisor
A_H_M_ImrulCommunity AdvisorAccepted solution
Community Advisor
November 25, 2023

@luis_ochoa 

An Instant in Java represents an instantaneous point in time on the timeline. It's stored in UTC and typically represented in the format of "yyyy-MM-ddTHH:mm:ss.SSSZ" where "T" separates the date and time, "Z" signifies UTC, and the time includes hours, minutes, seconds, and milliseconds. However, you can convert an Instant to a LocalDate (which represents just a date without time or timezone information) and then back to an Instant if needed.

 

Instant instant = Instant.now(); // Replace this with your specific Instant // Convert Instant to LocalDate LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate(); // Convert LocalDate back to Instant (with only date and no time) Instant dateOnlyInstant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); System.out.println(dateOnlyInstant); // Output: 2023-11-26T00:00:00Z

 

Thats the maximum you can achieve with this approach.  Otherwise, you can check this customizable solution: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/adobe/acs/commons/wcm/impl/SiteMapServlet.java 

kautuk_sahni
Community Manager
Community Manager
November 23, 2023

@luis_ochoa Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni
Flores-Lucas
April 8, 2024

Did you find a solution for outputting only the date (yyyy-MM-dd) on the property lastMod, without the time portion? I have been trying to reach that result with no success so far.

 

The idea would be to have it like this:

<url>
   <loc>https://www.mysite.com/mypage</loc>
   <lastmod>2023-09-11</lastmod>
</url>

 

Thank you in advance.