简体   繁体   English

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

[英]Dataweave 2.0 - Cannot coerce String to LocalDateTime

I get a CSV file with data that I transform to application/java. 我得到一个CSV文件,其中包含我转换为application / java的数据。

One of the fields (Creation_Date) is a DateTime field that I get as String because the output field is a string type. 字段(Creation_Date)之一是DateTime字段,我将其作为String获取,因为输出字段是字符串类型。

Input field: Creation_Date ( String ) - Example : 2019-03-02 07:00:00.000 输入字段: Creation_Date( String )- 示例 :2019-03-02 07:00:00.000

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

I use that code in my Dataweave 2.0 transformation because I want to add one hour more to the input datetime: 我在Dataweave 2.0转换中使用了该代码,因为我想在输入日期时间中再增加一小时:

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

But it returns an error: 但它返回一个错误:

 Cannot coerce a String to a Localdatetime, caused by CreatedDate

To add or modify parts of the data such as adding hours you should convert to LocalDateTime and then use a Period to add a specific Period of time to the datetime. 要添加或修改部分数据(例如添加小时数),应将其转换为LocalDateTime,然后使用“期间”将特定时间段添加到日期时间。 Also need to as milliseconds to format based on your expected input/output. 还需要几毫秒的时间来根据您期望的输入/输出进行格式化。 Try this, but change pretendPayload to payload for your example: 尝试此操作,但是将您的示例的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" }
}

Info on Period here: https://docs.mulesoft.com/mule-runtime/4.1/dataweave-types#dw_type_dates_period 此处有关期间的信息: https : //docs.mulesoft.com/mule-runtime/4.1/dataweave-types#dw_type_dates_period

You can use the now() in dataweave 2.0. 您可以在dataweave 2.0中使用now()。 Check the URL: https://docs.mulesoft.com/mule-runtime/4.1/dw-core-functions-now . 检查URL: https : //docs.mulesoft.com/mule-runtime/4.1/dw-core-functions-now

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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