Expand my Community achievements bar.

SOLVED

How to compare the cq:lastModified and cq:lastReplicated values in AEM?

Avatar

Level 2

Hi,

There is a requirement to query cq:lastModified and cq:lastReplicated values. We need to fetch the data and  check which date is greater than another.

 

 

Mano4_0-1685369016303.png

Will it be possible to compare and check the values from the above time format? Kindly share your inputs.

 

Regards,

Manoj

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

HI @Mano4 

 

Yes, you can compare the date in java. Below is the example: https://www.tutorialspoint.com/how-to-compare-two-dates-in-java

 

Thanks,

Kiran Vedantam.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

HI @Mano4 

 

Yes, you can compare the date in java. Below is the example: https://www.tutorialspoint.com/how-to-compare-two-dates-in-java

 

Thanks,

Kiran Vedantam.

Avatar

Community Advisor

hi @Mano4 ,

 

Yes, it is possible to compare and check the values of cq:lastModified and cq:lastReplicated in AEM. Both cq:lastModified and cq:lastReplicated store the timestamp of when the content was last modified and last replicated, respectively. To compare the values, you can retrieve the properties using the JCR API or the Sling API in AEM. Once you have retrieved the values, you can compare them using standard Java Date or Calendar objects.

 

Here's an example code snippet using the Java Calendar object to compare cq:lastModified and cq:lastReplicated:

// Assuming you have a resource object representing the content node
ValueMap valueMap = resource.adaptTo(ValueMap.class);

// Get the cq:lastModified and cq:lastReplicated values as Calendar objects
Calendar lastModified = valueMap.get("cq:lastModified", Calendar.class);
Calendar lastReplicated = valueMap.get("cq:lastReplicated", Calendar.class);

// Compare the values
if (lastModified != null && lastReplicated != null) {
    if (lastModified.after(lastReplicated)) {
        // cq:lastModified is greater than cq:lastReplicated
        // Perform your logic here
    } else if (lastModified.before(lastReplicated)) {
        // cq:lastModified is earlier than cq:lastReplicated
        // Perform your logic here
    } else {
        // cq:lastModified and cq:lastReplicated are the same
        // Perform your logic here
    }
} else {
    // Handle the case where either cq:lastModified or cq:lastReplicated is null
    // Perform your logic here
}

By comparing the Calendar objects, you can determine which date is greater or perform any custom logic based on your requirements.

Note that the above code assumes you have a resource object representing the content node and that you have appropriate access to retrieve the properties.