1

Boomi/Java/Groovy noob here... I'm starting with a date (that's sent to us from a 3rd party vendor) that's formatted like this: 2018-04-18 12:15:00.000000 (no 'T') which we've been told is in the America/Chicago TZ. What I ultimately need, is to get my output in the following date format (with the 'T', and an offset added):

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

-or-

2018-04-18T12:15:00.000000-05:00 (depending on whichever is the local time for that particular time of year in Chicago)

I've floundered around trying numerous combinations of SimpleDateFormat, ZoneID.of, ZonedDateTime.ofInstant, LocalDateTime.ofInstant, LocalDateTime.parse, etc... So, far I've failed miserably in finding the correct combination.

Any help would be greatly appreciated!!

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Squish88
  • 25
  • 3

1 Answers1

0

You can read it as a LocalDateTime, then move it to being the same time in the Chicago Timezone:

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

prints:

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

If you need it just as an OffsetDateTime, then you can use the method toOffsetDateTime()

println chicagoZonedTime.toOffsetDateTime()

Which prints:

2018-04-18T12:15-05:00
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Nice answer, thanks. Only `.n` in your format pattern string is wrong. `n` is for nanosecond and only correct when there are nine decimals on the seconds. Here are six decimals, so you need `.SSSSSS` instead (when the decimals are `000000`, you don’t discover the error, of course). – Ole V.V. Sep 29 '19 at 10:33