0

My input is a .csv format file with a date of birth column which contains strings such as

Aug-03-2015

I want to change this coloumn to a date (if not possible then at least to another string) format as below

03-08-2015

. Tried the logic suggested by RyanCarter. I have now tried using

Birthday:$.DOB as :date {format : "MMM-dd-yyyy"} as :string {format: "dd/MM/yyyy"}

where DOB is a string column in the .csv input. But i got the below exception.

Message               : Exception while executing: 
DOB:$.DOB as :date {format : "MMM-dd-yyyy"} as :string {format: "dd/MM/yyyy"}
      ^

Cannot coerce a :string to a :date, caused by :Text 'NOV-03-1992' could not be parsed at index 0
Type                  : com.mulesoft.weave.mule.exception.WeaveExecutionException
Code                  : MULE_ERROR--2
********************************************************************************

Exception stack is:
1. Cannot coerce a :string to a :date, caused by :Text 'NOV-03-1992' could not be parsed at index 0 

(com.mulesoft.weave.model.values.coercion.exception.UnsupportedTypeCoercionException)

com.mulesoft.weave.model.values.coercion.LocalDateTypeCoercionValue:30 (null)
2. Exception while executing: 
DOB:$.DOB as :date {format : "MMM-dd-yyyy"} as :string {format: "dd/MM/yyyy"}
      ^
Cannot coerce a :string to a :date, caused by :Text 'NOV-03-1992' could not be parsed at index 0 (com.mulesoft.weave.mule.exception.WeaveExecutionException)

com.mulesoft.weave.mule.WeaveMessageProcessor$WeaveOutputHandler:166 (null)

********************************************************************************

Root Exception stack trace:
com.mulesoft.weave.model.values.coercion.exception.UnsupportedTypeCoercionException: Cannot coerce a :string to a :date, caused by :Text 'NOV-03-1992' could not be parsed at index 0

1 Answer 1

1

Convert to a date firt using the date format, then back to a String using the second date format:

varOutput: 'Aug-03-2015'  as :date {format : "MMM-dd-yyyy"} as :string {format: "dd/MM/yyyy"}
Sign up to request clarification or add additional context in comments.

4 Comments

Instead of using 'Aug-03-2015' directly i have tried extracting the value from the input metadata as $.DOB where DOB is the column name in input. I got a "Cannot coerce a :string to a :date" error. Instead of writing the string in data weave how do i pickup the value from the input?
Its probably not in the correct format. Can you post the stack trace with the date its trying to parse?
Added the exception stack trace in the question.
My bad. I have now changed the input to "Nov-03-1992" and it works . Thanks for the hlep :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.