Expand my Community achievements bar.

SOLVED

file.setLastModified() method in ResourceTree SitemapGenerator

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

View solution in original post

3 Replies

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

@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