Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

AEM 6.4 Update a datetime field changing the valueFormat and value

NitroHazeDev
Level 7
Level 7

Hello All,

 

Is there a way to update a datetime field with new values?

If existing value includes a valueformat yyyy-MM-dd'T'HH:mm:ss as persisted in aem and displayed as and I want to just set the value within aem to be 2015-07-26. That is plain yyyy-mm-dd. I do not want to create a new field but use the existing field within a dialog.

Any ideas how i could approach this?

 

I can set it to blank but not with any other value . 

1 Accepted Solution
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi,

AEM uses date format yyyy-MM-dd'T'HH:mm:ss to store the date/calender type in JCR.

When you read this value , you can skip the part after yyyy-MM-dd

String dateStr = dateToString(jcrDate, "yyyy-MM-dd");

/**
   * Get string from date for the given format
   * 
   * @param date
   * @param format
   * @return date string
   */
public static String dateToString(Calendar date, String format) {
    if (date == null || StringUtils.isBlank(format)) {
      return StringUtils.EMPTY;
    }
    try {
      DateFormat dateFormat = new SimpleDateFormat(format);
      return dateFormat.format(date.getTime());
    }
    catch (Exception e) {
      LOG.error("Not able to convert date to string : {}", e.getMessage());
    }
    return StringUtils.EMPTY;
  }

 

What is your use case here?

View solution in original post

4 Replies
DEBAL_DAS
Level 5
Level 5

Please review https://blogs.perficient.com/2018/10/18/a-way-to-handle-dates-in-aem/ , I hope this one will help you.

 

Please refer the below screenshot the way I have defined the date field in demo component 

 

DEBAL_DAS_0-1640792066139.png

 

The value was stored in 'YYYY-MM-DD' format as shown below

DEBAL_DAS_1-1640792283695.png

I am using AEM 6.5.9.

NitroHazeDev
Level 7
Level 7

Hi @DEBAL_DAS , I wish i could use the approach but requirement is to reuse a date time field with a value format which has hhmmss and then if checkbox is checked, pick only yyyy-mm-dd, hiding the time portion on the pop up.

I am unable to set any value to it  other than setting it to blank, maybe cause of the format associated to the field yyyy-mm-dd.

Do you know how i could approach this?

 

 

 

 

 

Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi,

AEM uses date format yyyy-MM-dd'T'HH:mm:ss to store the date/calender type in JCR.

When you read this value , you can skip the part after yyyy-MM-dd

String dateStr = dateToString(jcrDate, "yyyy-MM-dd");

/**
   * Get string from date for the given format
   * 
   * @param date
   * @param format
   * @return date string
   */
public static String dateToString(Calendar date, String format) {
    if (date == null || StringUtils.isBlank(format)) {
      return StringUtils.EMPTY;
    }
    try {
      DateFormat dateFormat = new SimpleDateFormat(format);
      return dateFormat.format(date.getTime());
    }
    catch (Exception e) {
      LOG.error("Not able to convert date to string : {}", e.getMessage());
    }
    return StringUtils.EMPTY;
  }

 

What is your use case here?

NitroHazeDev
Level 7
Level 7

Thanks @Arun_Patidar .. If date time field was in a  format with hhmmss, i had to fill in 00:00.I went ahead with creating a date field and storing it in hh mm ss but displaying in dd-mm-yyyy format, and while copying i had to clear the date time field and then set it with the value persisted.