user3165854
user3165854

Reputation: 1655

How to get current date in Mule workflow

I am looking to get the current date in a workflow so I can append it to a filename I am writing to a folder.

When I have looked around I can only see how to do it in dataweave but this does not look like it will work in the configuration of other components outside dataweave.

How can I do this?

Thanks

Upvotes: 0

Views: 12256

Answers (2)

aled
aled

Reputation: 25802

In Mule 3 you can do it easily with DataWeave and assign it to a variable or the payload, though the XML is a little more verbose than MEL expression in the answer by utechtzs.

As an example of how to create a filename with the date and assign the string to a variable:

<dw:transform-message doc:name="Transform Message">
  <dw:set-variable variableName="filename"><![CDATA[%dw 1.0
     %output application/java
     ---
     "myfile-" ++ (now as :string {format: "yyyyMMddHHmmssSSS"})]]>
  </dw:set-variable>
</dw:transform-message>
<logger level="INFO" doc:name="Logger" message="#[flowVars.filename]"/>

In Mule 4 DataWeave replaces MEL in expressions and you can use it directly. Example: #["myfile-" ++ (now() as String {format: "yyyyMMddHHmmssSSS"})]

Both examples return the same Java string. Example: "myfile-20181117150935499"

Upvotes: 1

utechtzs
utechtzs

Reputation: 1023

#[server.dateTime.format('yyyyMMddHHmmssSSS')] or any other format you'd like.

Documentation: https://docs.mulesoft.com/mule-runtime/3.9/mule-expression-language-date-and-time-functions#obtaining-the-server-time

Upvotes: 0

Related Questions