97008 48403
+91 40-27177600

Blog

Finding Current dateTime and setting default dateTime in DataWeave of Mule ESB

single-blog

From the technical center of excellence of Massil Technologies, our technology leader Srini makes it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog addresses how to set default date and time in Mule ESB data wave. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

The DataWeave in Mule ESB is a technically empowered template that allows a user to transform data from one format to any kind of format like XML, CSV, JSON, Pojos, Maps so on. The DataWeave empowers Mule ESB to make complex data transformations with ease and makes it stand out from the competition.

For finding the current date and time and setting a default DateTime in DataWeave you need to understand the fields in Context Object Server related to date and time. There are two fields namely DateTime and nanoTime.

Finding Current Date and Time on Context Object Server:

For example, the following basic syntax returns you with date and time on the Context Object Server: #[server.dateTime]. Using the following syntax you can further define the data you wish to extract from the server

#server.dateTime.getMilliSeconds()

#server.dateTime.getseconds()

#server.dateTime.getminutes()

#server.dateTime.gethours()

#server.dateTime.getdayOfWeek()

#server.dateTime.getdayOfMonth()

#server.dateTime.getdayOfYear()

#server.dateTime.getmonth()

#server.dateTime.getyear()

The above listed syntax enables you to perform certain operations with dateTime.

Setting default datetime in DataWeave:

  1. date : now as :datetime {format: “yyyy-MM-dd HH:mm:ss.SSS”}
  2. departureDate: now as :string {format:”yyyyMMdd”}

You can use the below expression to get the current data and time in DataWeave.

“datatime” : now()

you can also use – “defaultDate” : now as :string {format: “yyyy-MM-dd”} for getting system date and time.

Use the below Mule flow to set the default dateTime:

<flow name=“muledataweaveFlow1”>

<http:listener config-ref=“HTTP_Listener_Configuration” path=“/getDate” allowedMethods=“GET” doc:name=“HTTP”/>

<dw:transform-message doc:name=“Transform Message”>

<dw:set-payload><![CDATA[%dw 1.0

%output application/json

{

a: now,

b: now.day,

c: now.minutes

}]]></dw:set-payload>

</dw:transform-message>

</flow>

The output of the above Mule Flow will be

{

“a”: “2017-09-12T18:36:18.389+05:30”,

“b”: 12,

“c”: 36

}

Hope this piece of code and article from Massil Technologies was useful for you to find the current timeDate and setting the default timeDate on DataWeave. If you have any further queries on this topic please reach out to info@massil.tech.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Get in touch with us

Scroll to top