Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Does any one able to retrieve cq:lastReplicated in JSON as String?

Avatar

Level 1

Hi,

 

Does anyone able to retrieve cq:lastReplicated in JSON output as String? I am using Query builder to retrieve query result.

 

ValueMap properties = hit.getProperties()

 

if (properties.get("cq:lastReplicated") != null) {
LOGGER.info("Published Date" + properties.get("cq:lastReplicated").toString());
contentObject.addProperty("publishDate", properties.get("cq:lastReplicated").toString());

}



Output coming as :
Output coming inJSON as :
 
"publishDate""java.util.GregorianCalendar[time=1707236418257,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id=\"GMT-05:00\",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2024,MONTH=1,WEEK_OF_YEAR=6,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=37,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=20,SECOND=18,MILLISECOND=257,ZONE_OFFSET=-18000000,DST_OFFSET=0]",
Please let me know if I am missing any steps .






Topics

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

3 Replies

Avatar

Administrator

@kapil_rajoria @HeenaMadan @Umesh_Thakur @h_kataria When you have a chance, could you please review this question and offer your perspective? I'd love to hear your insights.



Kautuk Sahni

Avatar

Community Advisor

Hi @pmhaem ,

It seems you are getting a bit differently, use proper get method like:

Date lastReplcationDate = properties.get("cq:lastReplicated", Date.class);
here you can use either dataFormate or DateTimeFormatter to convert
the date to string before adding it to JSON.

you can refer below link:

 https://stackoverflow.com/questions/5683728/convert-java-util-date-to-string

Hope this helps

Umesh Thakur

Avatar

Community Advisor

Hi @pmhaem 

 

Use below to get cq:lastReplicated value and then convert date to string as per your dateformat before adding to jsonobject.

 

ValueMap properties = hit.getProperties();
if (properties.get("cq:lastReplicated") != null) {
java.util.Date replicatedDate = properties.get("cq:lastReplicated", Date.class);
//now convert Date to string
Strig strDate = convertDateToString(replicatedDate);
}

public static String convertDateToString(Date input){
        SimpleDateFormat outputFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z");
        outputFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            String formattedDateStr = outputFormatter.format(input);
            System.out.println("Formatted Date String: " + formattedDateStr);
        return formattedDateStr;
    }

 

 
then string date : 2024-09-17 20:02:03.470 +0000
Hope this helps!