简体   繁体   中英

How to convert a String to Datetime using Dataweave Mule

I have the following string 20170912113456 and i want to convert it to this format 2017/09/12 11:34:56 using Dataweave Mule. Any ideas about how to do it ?

I would recommend first converting your input date string into a Dataweave localdatetime object. Then you can output the date object back to a string of any format.

%dw 1.0
%output application/json
---
formattedDate: flowVars.date as :localdatetime{format: "yyyyMMddHHmmss"} as :string{format:"yyyy/mm/dd HH:mm:ss"}

If you interested in learning the inner-working check out my converting string dates to Java Date Objects in Mule tutorial, https://youtu.be/XqI_Kii9RzA

You can try few things like formatting to data and then to string based on below example.

myDate: ((payload as :string) as :date {format: "yyyyMMdd"}) as :string {format: "MM-dd-yyyy"}

For a better understanding the Date Time operations please go through below link.

https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#date-time-operations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM