Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

AEM 6.4 Update a datetime field changing the valueFormat and value

Avatar

Level 10

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

Avatar

Correct answer by
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?



Arun Patidar

View solution in original post

4 Replies

Avatar

Employee Advisor

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.

Avatar

Level 10

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?

 

 

 

 

 

Avatar

Correct answer by
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?



Arun Patidar

Avatar

Level 10

Thanks @arunpatidar .. 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.