Get date as type date in cq5

I am using date field xtype in my component. I want to get the value from my dialog as date not string. Is there any method to get the value of the dialog in any other type other than string? How do I need a date here.

+3


source to share


4 answers


Yes. You can use the method get(java.lang.String name, java.lang.Class<T> type)

of the ValueMap class to get the value and convert it to the specified type.

i.e. If you want the value of your property to be Date, you can use the following syntax.

Date date = properties.get("date", Date.class);

      

You can also use the method get(java.lang.String name, T defaultValue)

to return a default value in case the property does not exist in the repository. For Ex



Date date = properties.get("date", new Date());
Date date = properties.get("text", "default_text");

      

The default is also used to determine the type of value conversion. that is, it will return a string if the default is a String, or it will return a Date object if the default is a Date object.

NOTE. The properties object is an instance of ValueMap

+11


source


You can also use the default. The default value you provide must match the type you expect to receive.

Date date = properties.get("date", new Date());

      



http://sling.apache.org/apidocs/sling5/org/apache/sling/api/resource/ValueMap.html

+4


source


When we use the date field in the cq5 dialog, its value is kept by default as specified in "Date" in CRX. By selecting the value of this property from the CRX in the CQ page, we must explicitly point the same to the java.util.Date object as stated below.

java.util.Date date = properties.get("publishDate",java.util.Date.class);

      

+1


source


I am going to revive this old question because I had the same problem and found a more elegant solution.

If you use the following configuration in your dialog for your field, you only get the date picker, but it will be stored as a date in CRX-DE:

xtype="datetime"
hideTime="{Boolean}true"

      

0


source







All Articles