Dataweave 2.0-无法将字符串强制为LocalDateTime

时间:2019-04-16 21:05:08

标签: mule dataweave string-to-datetime

我得到一个CSV文件,其中包含我转换为application / java的数据。

其中一个字段(Creation_Date)是我作为字符串获取的DateTime字段,因为输出字段是字符串类型。

输入字段:创建日期(字符串)-示例:2019-03-02 07:00:00.000

输出字段::CreatedDate( String )-示例:2019-03-02 08:00:00.000

我在Dataweave 2.0转换中使用该代码,因为我想在输入日期时间增加一小时:

CreatedDate: payload.Creation_date as LocalDateFormat {format: "yyyy-MM-dd HH:mm:ss+01:00"}

但是它返回一个错误:

 Cannot coerce a String to a Localdatetime, caused by CreatedDate

2 个答案:

答案 0 :(得分:3)

要添加或修改部分数据(例如添加小时数),应将其转换为LocalDateTime,然后使用Period将特定时间段添加到datetime。还需要几毫秒的时间来根据您期望的输入/输出进行格式化。尝试此操作,但将您的示例的pretendPayload更改为有效负载:

%dw 2.0
output application/json
var pretendPayload = {Creation_date: "2019-03-02 07:00:00.000"}

type LocalDateFormat = LocalDateTime { format: "yyyy-MM-dd HH:mm:ss.SSS" }
---
{
    CreatedDate: (pretendPayload.Creation_date as LocalDateFormat + |PT1H|) as String{format: "yyyy-MM-dd HH:mm:ss.SSS" }
}

有关期间的信息:https://docs.mulesoft.com/mule-runtime/4.1/dataweave-types#dw_type_dates_period

答案 1 :(得分:0)

You can use the now() in dataweave 2.0. Check the URL: https://docs.mulesoft.com/mule-runtime/4.1/dw-core-functions-now.

Hope this will help you.