Question

I have been trying to resolve this issue for the past day and can not seem to find a more elegant (Groovy style) solution, hope someone can help me out.

Basically I have a list that contains time stamps (GMT-0) coming from an external source that need to be converted to the specified time zone (in this case Paris GMT+2), then replaced the original time stamps in the list with the new time (GMT+2) in the epoch format.

Note: I have tried using the GregorianCalendar but have not managed to figure out how to set input time zone (GMT-0) so I can convert the imputed time to any time zone.

This is my ugly solution:

def tStamp= ['2012-06-14T20:16:20Z', '2012-06-14T20:16:40Z', '2012-06-14T20:17:00Z', '2012-06-14T20:17:20Z', '2012-06-14T20:17:40Z', '2012-06-14T20:18:00Z']
println "Ext: "+ tStamp
tStamp = tStamp.collect { 7200+(new Date().parse("yyyy-MM-dd'T'HH:mm:ss'Z'", it).time.toString().toLong()/1000).toInteger() }
println "New: "+ tStamp

Ext: [2012-06-14T20:16:20Z, 2012-06-14T20:16:40Z, 2012-06-14T20:17:00Z, 2012-06-14T20:17:20Z, 2012-06-14T20:17:40Z, 2012-06-14T20:18:00Z]
New: [1339704980, 1339705000, 1339705020, 1339705040, 1339705060, 1339705080]

New version:

def timeStamps =['2012-06-18T09:11:00Z', '2012-06-18T09:11:20Z', '2012-06-18T09:11:40Z']
println "ORIG: "+ timeStamps

// Import the external time: GMT-0
def inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
inputFormat.timeZone = TimeZone.getTimeZone('GMT-0')
timeStamps = timeStamps.collect { inputFormat.parse(it) }
println "IN: "+ timeStamps

// Convert the timez to GMT+2 and conver to epoch
timeStamps = timeStamps.collect { def gcal = new GregorianCalendar(TimeZone.getTimeZone("GMT+2")); gcal.setTime(it); return gcal.getTimeInMillis()/1000  }
println "OUT: "+ timeStamps

ORIG: [2012-06-18T09:11:00Z, 2012-06-18T09:11:20Z, 2012-06-18T09:11:40Z]
IN: [Mon Jun 18 11:11:00 CEST 2012, Mon Jun 18 11:11:20 CEST 2012, Mon Jun 18 11:11:40 CEST 2012]
OUT: [1340010660, 1340010680, 1340010700]

Does anyone have any suggestions?

Best regards and thanks in advance,

Sebastian

Était-ce utile?

La solution 2

I have finally found a solution to this problem:

  import java.text.SimpleDateFormat
  def convertTimeZone(String time, String sourceTZ, String destTZ) {
    final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"
    def sdf = new SimpleDateFormat(DATE_TIME_FORMAT)
    Date specifiedTime

    try {
      if (sourceTZ != null){
        sdf.setTimeZone(TimeZone.getTimeZone(sourceTZ))
      }else{
        sdf.setTimeZone(TimeZone.getDefault()) // default to server's timezone
      }
      specifiedTime = sdf.parse(time)
    } catch (Exception e1){
      try {
        specifiedTime = new Date().parse(DATE_TIME_FORMAT, time)
      } catch (Exception e2) {
        return time
      }
    }
    // switch timezone
    if (destTZ != null){
      sdf.setTimeZone(TimeZone.getTimeZone(destTZ))
    }else{
      sdf.setTimeZone(TimeZone.getDefault()) // default to server's timezone
    }

    new Date().parse("yyyy-MM-dd'T'HH:mm:ss'Z'", sdf.format(specifiedTime))
  }

 def timeStamps = ['2012-06-14T20:16:20Z', '2012-06-14T20:16:40Z']

 println "IN: "+ timeStamps
 timeStamps = timeStamps.collect { ((convertTimeZone(it,"GMT-0","Europe/Paris")).time.toString().toLong()/1000).toInteger() }
 println "OUT: "+ timeStamps

Output:

IN: [2012-06-14T20:16:20Z, 2012-06-14T20:16:40Z]
OUT: [1339712180, 1339712200]

Hope this solution is also helpful for someone else.

Autres conseils

You can use SimpleDateFormat to parse and format dates. Notice that Date objects don't know anything about time zones, so when working with Date objects you usually don't need to worry about those things, only then parsing them from strings or formatting them into strings.

import java.text.SimpleDateFormat

def timeStamps = ['2012-06-14T20:16:20Z', '2012-06-14T20:16:40Z']

def inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
inputFormat.timeZone = TimeZone.getTimeZone('GMT-0')

// Once parsed, dates are agnostic to time zones.
def dates = timeStamps.collect { inputFormat.parse(it) }

def outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z")
outputFormat.timeZone = TimeZone.getTimeZone('GMT+2')

println "Dates in current locale: " + dates
println "Dates in GMT+2 time zone: " + dates.collect { outputFormat.format(it) }

Output:

Dates in current locale: [Thu Jun 14 17:16:20 ART 2012, Thu Jun 14 17:16:40 ART 2012]
Dates in GMT+2 time zone: [2012-06-14T22:16:20 +0200, 2012-06-14T22:16:40 +0200]

Notice that in the first line, dates are printed with the current locale (which is GMT-3 on my machine... that's why there is a 3 hour difference between them and the input GMT-0 dates).

Of course, you don't need the intermediate dates list, you can directly do:

timeStamps.collect { outputFormat.format(inputFormat.parse(it)) }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top