Groovy: How do you initialise and compare date/time values from different timezones?
datetime, groovy
Solution
Or, use the JodaTime package
@Grab( 'joda-time:joda-time:2.1' )
import org.joda.time.*
import org.joda.time.format.*
String a = "18-05-2012 09:29:41 +0800"
String b = "18-05-2012 09:29:21 +0900"
DateTimeFormatter dtf = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss Z" );
def start = dtf.parseDateTime( a )
def end = dtf.parseDateTime( b )
assert 1 == Hours.hoursBetween( end, start ).hours
Problem
I need to standardise and compare date/time fields that are in differnt timezones. eg How do you find the time difference between the following two times?... ``` "18-05-2012 09:29:41 +0800" "18-05-2012 09:29:21 +0900" ``` What's the best way to initialise standard varaibles with the date/time? The output needs to display the difference and normalised data in a timezone (eg +0100) that is different to the incoming values and different to the local environment. Expected Output: ``` 18-05-2012 02:29:41 +0100 18-05-2012 01:29:21 +0100 Difference: 01:00:20 ```