Forum Discussion

678's avatar
678
Regular Contributor
6 years ago
Solved

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 

  • 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
    

5 Replies

  • Radford's avatar
    Radford
    Super Contributor

    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
    
  • Lucian's avatar
    Lucian
    Community Hero

    Can you elaborate a little bit more on this?

     

    If you need to assert some result on this you probably need to use a groovy assertions. I might be able to help you with that.

    • 678's avatar
      678
      Regular Contributor

      Lucian 

       

      Yes, appreciate - can you provide some groovy piece for this 

      • richie's avatar
        richie
        Community Hero

        Hi 678

         

        Lucian was saying he might be able to help with the groovy if he knows a little more about what you're actually trying to do - examples of the request and the response you are working on, etc.

         

        cheers.

         

        richie