Search code examples
muledataweavemulesoftmule4anypoint-studio

Converting "yyyy-MM-dd'T'HH:mm:ss.SSSZ" timestamp to "yyyyMMdd"


Sample Input:

{
  "transactionDateTime": "2023-12-05T16:56:15.912Z"
}

DataWeave code:

%dw 2.0
import * from dw::core::Strings

var date = payload.transactionDateTime as LocalDateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"} as String {format: "yyyyMMdd"}
output application/json  
---
{
    "k": date
}

Below is the error:

Cannot coerce String (2023-12-05T16:56:15.912Z) to LocalDateTime, caused by: Unknown pattern letter: T

14| var CURRENTDATEOriginalPostDate = payload.transactionDateTime as LocalDateTime {format:"yyyy-MM-ddTHH:mm:ss.SSSZ"} as String {format:"yyyy-MM-dd"}
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Trace:
  at main::main (line: 14, column: 35)

Solution

  • In this case "Z" is not a literal. The date-time format character "X" (zone-offset 'Z' for zero) could be used.

    %dw 2.0
    output application/json
    ---
    { 
        k: payload.transactionDateTime as LocalDateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSX"} as String {format: "yyyyMMdd"}
    }
    

    Output:

    {
      "k": "20231205"
    }