Tips and tricks to handle dateTime formats in Mule 4
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) | Description | Example |
---|---|---|
y | Year of the era (BCE or CE) or calendar year | 2021 |
yy | Calendar Year limit to 2 digits | 21 |
Y | Week based year | 2021 |
YY | Week based year limit to 2 digits | 21 |
u | Calendar Year without era (BCE or CE) | 2021 |
uu | Calendar Year limit to 2 digits | 21 |
M | The month with no leading 0 | 8 |
MM | The month with leading 0 | 08 |
MMM | Month name with 3 characters | Aug |
MMMM | Complete Month name | August |
d | Day of the Month | 09 |
D | Day of the Year | 221 |
E | Abbreviated day of the Week | Mon |
EEEE | Day of the Week | Monday |
h | An hour in 12 hour clock (1-12) | 03 |
H | An hour in 24 hour clock (0-23) | 03 |
m | Minute of the hour | 37 |
s | Second of the Minute | 29 |
S | Fraction of the second | 5 |
a | AM/PM | PM |
Z | Timezone Offset | +0530 |
ZZZZ | Timezone Offset | GMT+05:30 |
References:
Please share your valuable feedback 😊😊😊
Comments
Post a Comment