rhl-mulesoft
rhl-mulesoft

Reputation: 7

Mule 4 - Parse DateTime as DD/MM/YYYY 24HH:MM:SS format?

I am stuck with a requirement where I need to parse incoming payload String 6-12-2019 2:13:30 to DateTime format DD/MM/YYYY 24HH:MM:SS

I tried few approaches but nothing seems to work here.

"6-12-2019 2:13:30" as DateTime {format : "d-M-yyyy h:m:ss"} this results in error stating...

"Cannot coerce String (6-12-2019 2:13:30) to DateTime, caused by: Text '6-12-2019 2:13:30' could not be parsed: Unable to obtain ZonedDateTime from TemporaAccessor: {SecondOfMinute=30, MicroOfSecond=0, MilliOfSecond=0, HourOfAmPm=2, MinuteOfHour=13, NanoOfSecond=0},ISO resolved to 2019-12-06 of type java.time.format.Parsed

Can someone please help me with this?

Thanks.

Upvotes: 0

Views: 6271

Answers (2)

Nikhil
Nikhil

Reputation: 1

You can try to cast the incoming date to your specific format. And then reformat as need. In below sample, we converted the date to MM/dd/yyyy format and the reformatted it to the required format i.e. y-MM-dd. (date1 as Date {format: "MM/dd/yyyy"}) as String {format: "y-MM-dd"}

Upvotes: 0

aled
aled

Reputation: 25699

Your input doesn't match the type DateTime. It is missing a timezone, and the 'h' format character implicates it is an AM/PM date, but it is missing it. A LocalDateTime doesn't need a timezone. You can either change the input and format or change the type.

Please find below some valid examples:

%dw 2.0
output application/json
---
{
    localDateTime: "6-12-2019 2:13:30 pm" as LocalDateTime {format : "d-MM-yyyy h:mm:ss a"},
    localDateTime24h: "6-12-2019 14:13:30" as LocalDateTime {format : "d-MM-yyyy H:mm:ss"},
    dateTime: "6-12-2019 2:13:30 +0500" as DateTime {format : "d-MM-yyyy H:mm:ss Z"}
}

Output:

{
  "localDateTime": "6-12-2019 2:13:30 PM",
  "localDateTime24h": "6-12-2019 14:13:30",
  "dateTime": "6-12-2019 2:13:30 +0500"
}

Upvotes: 3

Related Questions