0

我遇到了一个要求,我需要将传入的有效负载字符串解析6-12-2019 2:13:30为 DateTime 格式DD/MM/YYYY 24HH:MM:SS

我尝试了几种方法,但这里似乎没有任何效果。

"6-12-2019 2:13:30" as DateTime {format : "d-M-yyyy h:m:ss"} 这会导致错误说明...

"Cannot coerce String (6-12-2019 2:13:30) to DateTime, caused by: Text '6-12-2019 2:13:30' could not be parsed: Unable to obtain ZonedDateTime from TemporaAccessor: {SecondOfMinute=30, MicroOfSecond=0, MilliOfSecond=0, HourOfAmPm=2, MinuteOfHour=13, NanoOfSecond=0},ISO resolved to 2019-12-06 of type java.time.format.Parsed

有人可以帮我吗?

谢谢。

4

2 回答 2

3

您的输入与 DateTime 类型不匹配。它缺少时区,'h' 格式字符暗示它是 AM/PM 日期,但它缺少它。LocalDateTime 不需要时区。您可以更改输入和格式或更改类型。

请在下面找到一些有效的示例:

%dw 2.0
output application/json
---
{
    localDateTime: "6-12-2019 2:13:30 pm" as LocalDateTime {format : "d-MM-yyyy h:mm:ss a"},
    localDateTime24h: "6-12-2019 14:13:30" as LocalDateTime {format : "d-MM-yyyy H:mm:ss"},
    dateTime: "6-12-2019 2:13:30 +0500" as DateTime {format : "d-MM-yyyy H:mm:ss Z"}
}

输出:

{
  "localDateTime": "6-12-2019 2:13:30 PM",
  "localDateTime24h": "6-12-2019 14:13:30",
  "dateTime": "6-12-2019 2:13:30 +0500"
}
于 2019-12-19T13:06:52.107 回答
0

您可以尝试将传入日期转换为您的特定格式。然后根据需要重新格式化。在下面的示例中,我们将日期转换为MM/dd/yyyy格式并将其重新格式化为所需的格式,即y-MM-dd. (date1 as Date {format: "MM/dd/yyyy"}) as String {format: "y-MM-dd"}

于 2020-01-22T19:11:46.370 回答