mulemulesoftanypoint-studiomule4mule-esb

Date time conversion (input format yyyy-MM-dd HH:mm:ss.SSSSS) in Mule 4


Getting the following error when trying to convert date time to a specific string format:

Cannot coerce String (2023-02-07 08:23:00.167000) to LocalDateTime, caused by: Text '2023-02-07 08:23:00.167000' could not be parsed, unparsed text found at index 25

6| crtDate: dtTime as LocalDateTime {format : "yyyy-MM-dd HH:mm:ss.SSSSS"} as String {format:"yyyy-MM-dd'T'HH:mm:ss"}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Trace: at main::main (line: 6, column: 12)

%dw 2.0
var dtTime = '2023-02-07 08:23:00.167000'
output application/json
---
{
  crtDate: dtTime as LocalDateTime {format : "yyyy-MM-dd HH:mm:ss.SSSSS"} as String {format:"yyyy-MM-dd'T'HH:mm:ss"}  
}

Expected: 2023-02-07T08:23:00


Solution

  • The format string has 5 digits for the milliseconds however the input has 6 characters. Adding another S fixes it.

    Example:

    "yyyy-MM-dd HH:mm:ss.SSSSSS"