Search code examples
datetimetimezonemuledataweavestring-to-datetime

Dataweave 2 - Cannot coerce String to DateTime


I have a String as DateTime like this 2019-05-21 00:00:00.000.

This is the code that I use in Dataweave 2 to transform String to DateTime:

SourceDate: payload.Source_date as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"}

But it returns this error:

"Cannot coerce String (2019-05-21 00:00:00.000) to DateTime, caused by: Text '2019-05-21 00:00:00.000' could not be parsed at index 10

I need to use 'T' and Z to use the TimeZone automatically.

What could be the problem?


Solution

  • You can use LocalDateTime which will use the current timezone:

    %dw 2.0
    output application/json
    ---
    SourceDate: payload.Source_date as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss.SSS"}
    

    And you can add the timezone:

    SourceDate: payload.Source_date as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss.SSS"} >> "GMT+1"