0

I am working with SoupUI a i need to adjust a Date/time (UTC) that i get back in a response to a GMT Date/time. The date that i get back in the respone looks as followes:

2012-11-09T00:00:00+01:00

I would like to convert this to

2012-11-08T23:00:00Z

Unfortunatly i lack Java skils and therefore also Groovy skils to be able to do this on my own. i did a lot o searches on date convertions but until now i was still unable to find what i was looking for. i will keep searching. if i do manage to get the solution then i will post it here.

tim_yates
  • 167,322
  • 27
  • 342
  • 338
user1700478
  • 3
  • 1
  • 2
  • Welcome to Stack Overflow! We encourage you to [research your questions](http://stackoverflow.com/questions/how-to-ask). If you've [tried something already](http://whathaveyoutried.com/), please add it to the question - if not, research and attempt your question first, and then come back. –  Sep 27 '12 at 16:01

1 Answers1

3

Assuming there isn't a colon in the timezone portion, I believe this should work:

// Your input String (with no colons in the timezone portion)
String original = '2012-11-09T00:00:00+0100'

// The format to read this input String
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" )

// The format we want to output
def outFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" )
// Set the timezone for the output
outFormat.timeZone = java.util.TimeZone.getTimeZone( 'GMT' )

// Then parse the original String, and format the resultant
// Date back into a new String
String result = outFormat.format( inFormat.parse( original ) )

// Check it's what we wanted
assert result == '2012-11-08T23:00:00Z'

If there is a colon in the TimeZone, you'll need Java 7 for this task (or maybe a date handling framework like JodaTime), and you can change the first two lines to:

// Your input String
String original = '2012-11-09T00:00:00+01:00'

// The format to read this input String (using the X
// placeholder for ISO time difference)
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssX" )
tim_yates
  • 167,322
  • 27
  • 342
  • 338