file.setLastModified() method in ResourceTree SitemapGenerator | Community
Skip to main content
Level 2
September 10, 2024
Solved

file.setLastModified() method in ResourceTree SitemapGenerator

  • September 10, 2024
  • 1 reply
  • 800 views

While creating sitemap using org.apache.sling.sitemap api, I am able to create sitemap by using 

ResourceTreeSitemapGenerator class


but in my sitemap.xml file I am getting date in <lastmod>2024-08-29T11:07:56.308Z</lastmod> this format, while i want in <lastmod>2024-05-10</lastmod> format.
I tried to covert it by dateformatted but the issue with code is 

 

 

 

final Url url = sitemap.addUrl(fullUrl); final Calendar lastmod = Optional.ofNullable(page.getLastModified()) .orElse(page.getContentResource().getValueMap().get(JcrConstants.JCR_CREATED, Calendar.class)); if (lastmod != null) { Instant lastModifiedInstant = lastmod.toInstant(); // Define the desired date format DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Format the date according to your requirement (e.g., 2024-04-30) String formattedDate = lastModifiedInstant.atZone(ZoneId.systemDefault()).format(formatter); // Now, set the last modified date in the sitemap URL url.setLastModified(formattedDate); // Optionally, log or print the formatted date if needed COMMON_LOGGER.logInfo(CLASS_NAME, "Formatted Last Modified Date: " + formattedDate); }

 

 

 

in url.setLastModified method it expected date to be in Instant format.
Note: with Instant type date comes in ISO 8601 format while i only want to show  yyyy-MM-dd

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 EstebanBustamante

@akshayswamiii  As I mentioned in my first reply, you need to extend and create a custom implementation of the sitemap to use a method that accepts a String instead of an Instant object. Please refer to the link I shared. However, the out-of-the-box sitemap uses the recommended time format for sitemaps, which you can read about here: Google’s Best Practices for XML Sitemaps. I would advise against changing the format just for simplicity.

 

Hope this helps

1 reply

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
September 10, 2024

Hi, 

Yes, that's expected because the "url.setLastModified()" method expects anInstantobject. In Java, Instant represents a specific point in time in the UTC time scale. It does not retain any format for dates such as "yyyy-MM-dd". Instead, Instant is a precise representation of a moment, with nanosecond accuracy, but without any human-readable date formatting.  So you cannot use this sitemap implementation with a date format, you will have to extend and use your own to achieve that.

 

You can learn more about the default sitemap implementation and how to extend it here:

https://www.theaemmaven.com/post/aem-apache-sling-sitemap 

 

Hope this helps. 


Esteban Bustamante
Level 2
September 10, 2024

Hi @estebanbustamante 

So, how can I achieve that format ??

EstebanBustamante
Community Advisor and Adobe Champion
EstebanBustamanteCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
September 10, 2024

@akshayswamiii  As I mentioned in my first reply, you need to extend and create a custom implementation of the sitemap to use a method that accepts a String instead of an Instant object. Please refer to the link I shared. However, the out-of-the-box sitemap uses the recommended time format for sitemaps, which you can read about here: Google’s Best Practices for XML Sitemaps. I would advise against changing the format just for simplicity.

 

Hope this helps

Esteban Bustamante