Chris
Chris

Reputation: 3562

Groovy: How do you initialise and compare date/time values from different timezones?

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

Upvotes: 4

Views: 6643

Answers (3)

rdmueller
rdmueller

Reputation: 11052

import java.text.SimpleDateFormat

def dates = ["18-05-2012 09:29:41 +0800",
 "18-05-2012 09:29:21 +0900"].collect{
   new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z").parse(it)
}
def dayDiffFormatter = new SimpleDateFormat("HH:mm:ss")
dayDiffFormatter.setTimeZone(TimeZone.getTimeZone("UTC"))
println dates[0]
println dates[1]
println "Difference "+dayDiffFormatter.format(new Date(dates[0].time-dates[1].time))

wow. doesn't look readable, does it?

Upvotes: 6

Chris
Chris

Reputation: 3562

Solution:

  1. Groovy/Java Date objects are stored as the number of milliseconds after 1970 and so do not contain any timezone information directly
  2. Use Date.parse method to initialise the new date to the specified format
  3. Use SimpleDateFormat class to specify the required output format
  4. Use SimpleDateFormat.setTimeZone to specifiy the timezone of the output data
  5. By using European/London timezone rather than GMT it will automatically adjusts for day light savings time
  6. See here for a full list of the options for date time patterns

-

import java.text.SimpleDateFormat
import java.text.DateFormat

//Initialise the dates by parsing to the specified format
Date timeDate1 = new Date().parse("dd-MM-yyyy HH:mm:ss Z","18-05-2012 09:29:41 +0800")
Date timeDate2 = new Date().parse("dd-MM-yyyy HH:mm:ss Z","18-05-2012 09:29:21 +0900")

DateFormat yearTimeformatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z")
DateFormat dayDifferenceFormatter= new SimpleDateFormat("HH:mm:ss")  //All times differences will be less than a day

// The output should contain the format in UK time (including day light savings if necessary)
yearTimeformatter.setTimeZone(TimeZone.getTimeZone("Europe/London"))

// Set to UTC. This is to store only the difference so we don't want the formatter making further adjustments
dayDifferenceFormatter.setTimeZone(TimeZone.getTimeZone("UTC"))

// Calculate difference by first converting to the number of milliseconds
msDiff = timeDate1.getTime() - timeDate2.getTime()
Date differenceDate = new Date(msDiff)

println yearTimeformatter.format(timeDate1)
println yearTimeformatter.format(timeDate2)
println "Difference " + dayDifferenceFormatter.format(differenceDate)

Upvotes: 3

tim_yates
tim_yates

Reputation: 171154

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

Upvotes: 3

Related Questions