Forum Discussion

678's avatar
678
Regular Contributor
7 years ago

Can any one provide a snippet to compare different timezones

Basically I need to validate  Example  GMT +8.00 is 3.00PM =  EST 3.00AM    Appreciate if any one can help here 
  • Radford's avatar
    7 years ago

    As Lucian & richie have said it is rather unclear what you are actually are after. I'm assuming that you want to assert that two date/time strings in different time zones are the same time, if so here his some Groovy script to give you a starter for 10 as we don't know what your exact scenario is:

     

    import java.text.SimpleDateFormat
    
    // Parse date time strings to Groovy date objects
    def dateTime1 = Date.parse('dd/MM/yyyy HH:mm:ss z','29/05/2018 17:00:00 GMT')
    def dateTime2 = Date.parse('dd/MM/yyyy HH:mm:ss z','29/05/2018 12:00:00 EST')
    
    // Just for debug/demo purposes output date times in the UTC time standard
    def sdf = new SimpleDateFormat('dd-MMM-yyyy HH:mm:ss z')
    sdf.setTimeZone(TimeZone.getTimeZone('UTC'))
    log.info('Date/Time 1 = ' + sdf.format(dateTime1))
    log.info('Date/Time 2 = ' + sdf.format(dateTime2))
    
    // Assert date/times are the same
    assert dateTime1 == dateTime2