假設沒有在時區部分冒號,我認爲這應該工作:
// 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'
如果有是在時區冒號,你需要Java 7完成這個任務(或也許像JodaTime這樣的日期處理框架),並且您可以將前兩行更改爲:
// 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")
歡迎來到Stack Overflow!我們鼓勵你[研究你的問題](http://stackoverflow.com/questions/how-to-ask)。如果你已經[嘗試了某些東西](http://whathaveyoutried.com/),請將其添加到問題中 - 如果沒有,請先研究並嘗試您的問題,然後再回來。 – 2012-09-27 16:01:10