Search code examples
javajcraemsling

Setting a JCR Date property


I'm having some trouble writing properties of a cq:PageContent. releaseDate (type Date) is giving me some trouble. The following method is from a sling-junit test where I'm calling from a @Before method.

private void setNewsReleaseDate(Resource res, int month) 
         throws InvalidDateException {
    String date = "2014-%sT11:31:00.000-04:00"; //07-23
    Calendar rd = DateUtil.parseISO8601(String.format(date, "0"+month+"-23")); //iso8601Date
    ModifiableValueMap modMap = res.adaptTo(ModifiableValueMap.class);
    if (modMap != null) {
        modMap.put("releaseDate", rd); // this is a Date property
        /* also tried below, which also fails
         modMap.put("releaseDate", String.format(date, "0"+month+"-23"));              
         */
    }
}

I get this error...

Value for key releaseDate can't be put into node:java.util.GregorianCalendar[time=1406129460000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT-04:00",offset=-14400000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_.... ]


Solution

  • I changed the method by passing in the page, and then get the /jcr:content below the page that I wanted to edit the properties of...

    private void setNewsReleaseDate(Page page, int month) throws InvalidDateException{
        LOGGER.info("change prop at page path "+page.getPath());
        Resource res = rr.getResource(page.getPath()+"/jcr:content");
        String date = "2014-%sT11:31:00.000-04:00"; //07-23
        Calendar rd = DateUtil.parseISO8601(String.format(date, "0"+month+"-23")); //iso8601Date        
        ModifiableValueMap modMap = res.adaptTo(ModifiableValueMap.class);
        if (modMap != null) {
            modMap.put("releaseDate", rd); // this is a Date property
            modMap.put("notes", "hello"); // a string property  
        }
    }