Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM 6.5 Sitemap lastmod date formatting

Avatar

Level 2

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-...

 

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.

 

 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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/ad... 

View solution in original post

5 Replies

Avatar

Community Advisor

@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");
}

 

Avatar

Level 2

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.

Screenshot 2023-11-23 at 9.01.26.png


Is there any other alternative I can follow?.

regards

 

Avatar

Correct answer by
Community Advisor

@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/ad... 

Avatar

Administrator

@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

Avatar

Level 1

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.