DecipherMiddleware

Tips and tricks to handle dateTime formats in Mule 4

· 513 words · 3 minutes to read · ✍️ Pranav Davar
Categories: MuleSoft
Tags: DATAWEAVE

We generally come across various scenarios while developing integrations, wherein we have to deal with multiple formats of date, time, or DateTime. Sometimes source systems can send date data in only one format which may or may not be accepted by target systems, which we have to handle using dataweave expressions. In this blog, we will be going through a couple of generic scenarios which we come across frequently. For example, if we use now() in dataweave, we get an output as below.

DataWeave expression:

%dw 2.0
output application/json
---
now()

Output:

"2021-12-12T14:27:48.064709Z"

The above output is in the format of DateTime, and from this, it is easy to convert or extract and date or time values.

Scenario #1 🔗

What if we get date time in some different format, for e.g. “2021/12/12 14.27.48 " and we just need to extract date and time in a different format as “12-12-2021 14:27:48 “. The best way is to cast the input date string to LocalDateTime by providing format and then convert to the string by providing the desired format.

DataWeave expression:

%dw 2.0
output application/json
---
"2021/12/12 14.27.48" as LocalDateTime {format:"yyyy/MM/dd HH.mm.ss"} as String {format:"dd-MM-yyyy HH:mm:ss"} 

Output:

"12-12-2021 14:27:48"

Scenario #2 🔗

Sometimes, we have to insert dateTime to databases like SQL Server, Oracle database, etc. The generally accepted format for the database date column through Mule4 is “yyyy-MM-dd HH:mm:ss "

DataWeave expression:

%dw 2.0
output application/json
---
now() as String {format:"yyyy-MM-dd HH:mm:ss"} 

Output:

"2021-12-12 14:27:48"

Scenario #3 🔗

What if we want to extract year and append Y in the end to output for example “2021Y”. If we use the format as “yyyyY” or “yY”, the output will come as a year repeated 2 times. The reason is y or Y is formatting character. If we want explicit Y to be displayed, one way could be to extract Year and then concatenate year with Y. Another way is to escape the formatting character by enclosing it in ‘’. For example:

DataWeave expression:

%dw 2.0
output application/json
---
now() as String {format:"yyyy'Y'"} 

Output:

"2021Y"

Tabular representation of various formatting characters that can be used.

DateTime: “2021-08-09T15:37:29.519633+05:30”

Character(s)DescriptionExample
yYear of the era (BCE or CE) or calendar year2021
yyCalendar Year limit to 2 digits21
YWeek based year2021
YYWeek based year limit to 2 digits21
uCalendar Year without era (BCE or CE)2021
uuCalendar Year limit to 2 digits21
MThe month with no leading 08
MMThe month with leading 008
MMMMonth name with 3 charactersAug
MMMMComplete Month nameAugust
dDay of the Month09
DDay of the Year221
EAbbreviated day of the WeekMon
EEEEDay of the WeekMonday
hAn hour in 12 hour clock (1-12)03
HAn hour in 24 hour clock (0-23)03
mMinute of the hour37
sSecond of the Minute29
SFraction of the second5
aAM/PMPM
ZTimezone Offset+0530
ZZZZTimezone OffsetGMT+05:30

References: 🔗

https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-format-dates

Please share your valuable feedback 😊😊😊