设置JCR日期属性

时间:2014-11-22 17:00:35

标签: java jcr aem sling

我在编写cq:PageContent的属性方面遇到了一些麻烦。 releaseDate(类型日期)给了我一些麻烦。以下方法来自sling-junit测试,其中我使用@Before方法进行调用。

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

我收到此错误...

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

1 个答案:

答案 0 :(得分:0)

我通过传入页面更改了方法,然后获取了我想要编辑属性的页面下面的/jcr:content ...

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