转换为正确的日期时间格式 - dataweave

发布于 2025-01-13 20:37:23 字数 448 浏览 0 评论 0原文

我有一个如下格式的请求

{
   "updatedTime": "Mon Mar 14 15:34:47 NZDT 2022"
}

响应应转换为“yyyy-MM-dd'T'HH:mm:ss”

{
"updatedTime": "2022-03-14'T'15:34:47"
}

我还参考了 Mule 文档 https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-format-dates。有什么想法吗?感谢

使用 Mule4、Dataweave 2.0

I have a request coming as below format

{
   "updatedTime": "Mon Mar 14 15:34:47 NZDT 2022"
}

The response should be converted to "yyyy-MM-dd'T'HH:mm:ss"

{
"updatedTime": "2022-03-14'T'15:34:47"
}

I also referred to Mule docs https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-format-dates. Any thoughts? Thanks

Using Mule4, Dataweave 2.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苹果你个爱泡泡 2025-01-20 20:37:23

您应该使用您共享的文档链接中的模式字符并构建整个日期的模式。您还可以搜索有关 Java 时间包模式的示例,DataWeave 日期/时间模式正是基于这些模式。

可能不太明显的是使用 patter zz 表示时区,使用 eee 表示本地化日期名称。

%dw 2.0
output application/json
---
{ 
    updatedTime: payload.updatedTime 
        as DateTime { format: "eee MMM dd HH:mm:ss zz yyyy" } 
        as LocalDateTime
}

You should use the patterns characters in the documentation link you shared and construct the pattern for the whole date. You can also search for examples on the Java time package patterns, which are the ones in which DataWeave date/time patterns are based on.

The ones that perhaps are not obvious are to use patter zz for the timezone and eee for the localized day name.

%dw 2.0
output application/json
---
{ 
    updatedTime: payload.updatedTime 
        as DateTime { format: "eee MMM dd HH:mm:ss zz yyyy" } 
        as LocalDateTime
}
最佳男配角 2025-01-20 20:37:23

转换给定输入的另一种方法是

%dw 2.0
output application/json
---
payload.updatedTime as DateTime {format: "EEE MMM dd HH:mm:ss z yyyy"} 
as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss"}    

在此处输入图像描述

Another way of transforming the given input is

%dw 2.0
output application/json
---
payload.updatedTime as DateTime {format: "EEE MMM dd HH:mm:ss z yyyy"} 
as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss"}    

enter image description here

美人骨 2025-01-20 20:37:23

虽然 @aled 的答案是正确的,但我真的很喜欢 从 Dataweave 更新运算符,然后您可以重写 Dataweave,如下所示:

%dw 2.0
output application/json
---
payload update {
    case .updatedTime -> $ as DateTime { format: "eee MMM dd HH:mm:ss zz yyyy" } as LocalDateTime
}

Dataweave Playground​​

While the answer of @aled is correct I really like the update operator from Dataweave and than you can rewrite the Dataweave like:

%dw 2.0
output application/json
---
payload update {
    case .updatedTime -> $ as DateTime { format: "eee MMM dd HH:mm:ss zz yyyy" } as LocalDateTime
}

Dataweave Playground

末蓝 2025-01-20 20:37:23

尝试这种方法日期解析

%dw 2.0
output application/json
---
"updatedTime":(payload.updatedTime as String replace "NZDT" with "GMT")  as DateTime {format: "EEE MMM dd HH:mm:ss O yyyy"} as LocalDateTime

输出

{
  "updatedTime": "2022-03-14T15:34:47"
}

Try this approach Date-Parsing

%dw 2.0
output application/json
---
"updatedTime":(payload.updatedTime as String replace "NZDT" with "GMT")  as DateTime {format: "EEE MMM dd HH:mm:ss O yyyy"} as LocalDateTime

Output

{
  "updatedTime": "2022-03-14T15:34:47"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文