向 Boomi 中没有的日期字符串添加偏移量 - Java/"Groovy" 脚本映射函数

Adding an offset to a date string that doesn't have one in a Boomi - Java/"Groovy" scripting map function

Boomi/Java/Groovy noob here...我从一个日期(从第 3 方供应商发送给我们)开始,格式如下:2018-04-18 12:15:00.000000 (没有 'T')我们被告知在 America/Chicago TZ 中。我最终需要的是按照以下日期格式获取我的输出(使用 'T',并添加了一个偏移量):

2018-04-18T12:15:00.000000-06:00

-或-

2018-04-18T12:15:00.000000-05:00 (取决于芝加哥一年中特定时间的本地时间)

我尝试了 SimpleDateFormat、ZoneID.of、ZonedDateTime.ofInstant、LocalDateTime.ofInstant、LocalDateTime.parse 等的多种组合...到目前为止,我已经在找到正确的组合时悲惨地失败了。

任何帮助将不胜感激!!

您可以将其读取为 LocalDateTime,然后将其移动到芝加哥时区的同一时间:

import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

String input = "2018-04-18 12:15:00.000000"
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n")
ZoneId chicago = ZoneId.of("America/Chicago")

LocalDateTime localTime = LocalDateTime.parse(input, format)
ZonedDateTime chicagoTime = localTime.atZone(chicago)

println chicagoTime

打印:

2018-04-18T12:15-05:00[America/Chicago]

如果您只需要它作为 OffsetDateTime,那么您可以使用方法 toOffsetDateTime()

println chicagoZonedTime.toOffsetDateTime()

打印:

2018-04-18T12:15-05:00