Search code examples
javadatetimegroovytimezone-offsetboomi

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... 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!!


Solution

  • 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