Groovy: How do you initialize and compare date / time values ​​from different time zones?

I need to standardize and compare date / time fields that are in different time zones . for example, how do you find the time difference between the next two times? ...

"18-05-2012 09:29:41 +0800" "18-05-2012 09:29:21 +0900" 

What is the best way to initialize standard date / time cookers? The output should display the difference and normalized data in the time zone (for example, +0100), which is different from the input values ​​and different from the local environment.

Expected Result:

 18-05-2012 02:29:41 +0100 18-05-2012 01:29:21 +0100 Difference: 01:00:20 
+4
source share
3 answers

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 
+3
source
 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?

+6
source

Decision:

  • Groovy / Java Date objects are stored as milliseconds after 1970 and therefore do not contain time zone information directly.
  • Use the Date.parse method to initialize a new date in the specified format
  • Use the SimpleDateFormat class to specify the required output format.
  • Use SimpleDateFormat.setTimeZone to specify the time zone on the output data
  • Using the European / London time zone, not GMT, it will be automatically configured to save time.
  • See here for a complete list of options for date templates.

-

 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) 
+3
source

Source: https://habr.com/ru/post/1414101/


All Articles