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