Mule Dataweave:无法将:string强制转换为:localdatetime

时间:2018-09-25 05:49:45

标签: datetime mule dataweave

尝试转换为日期时间时出现此错误的人:

Exception while executing: 
 utcTime: (payload as :localdatetime {format: "yyyy-MM-dd'T'HH:mm:ssZ" } >> "UTC")    
              ^
 Cannot coerce a :string to a :localdatetime, caused by :Text '2018-09-25T13:09:16.258Z' could not be parsed at index 19.

以下是我的代码:

     <flow name="testFlow">
         <http:listener config-ref="HTTP_Listener_Configuration" path="/datetime" doc:name="HTTP"/>
         <set-payload value="#[server.dateTime.format(&quot;yyyy-MM-dd'T'HH:mm:ss.SSS'Z'&quot;)]" doc:name="Set Payload"/>
         <dw:transform-message doc:name="Transform Message">
             <dw:set-payload><![CDATA[%dw 1.0
 %output application/json
 ---
 {
 utcTime: (payload as :localdatetime {format: "yyyy-MM-dd'T'HH:mm:ssZ" } >> "UTC")    
 }

 ]]></dw:set-payload>
         </dw:transform-message>
         <logger message="#[payload]" level="INFO" doc:name="Logger"/>
     </flow>

2 个答案:

答案 0 :(得分:1)

您的原始格式与您尝试转换的格式不同(缺少毫秒):

yyyy-MM-dd'T'HH:mm:ss.SSSZyyyy-MM-dd'T'HH:mm:ssZ

答案 1 :(得分:0)

设置有效负载时,请输入文字字符“ Z”(yyyy-MM-dd'T'HH:mm:ss.SSS'Z')。因此,您可以将字面量翻译回去:

(payload as :localdatetime{format: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"} as :string {format: "yyyy-MM-dd'T'HH:mm:ss"} >> "UTC")

或者,您可以考虑时区:

<flow name="testFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/datetime" doc:name="HTTP"/>
    <set-payload value="#[server.dateTime.format(&quot;yyyy-MM-dd'T'HH:mm:ss.SSSZ&quot;)]" doc:name="Set Payload"/>
    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    utcTime: (payload as :localdatetime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ" } as :string {format: "yyyy-MM-dd'T'HH:mm:ss"} >> "UTC")    
}

]]></dw:set-payload>
    </dw:transform-message>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>