Reputation: 11
Below is my response code from a rest request
"createdDate" : "2012-02-27T18:10:28-0500",
"isDeleted" : "indeterminate",
"modifiedDate" : "2018-07-18T01:40:05-0400"
I am trying to convert a time stamp of format 2018-07-18T01:40:05-0400
to MM/dd/YYYY hh:mm
-0400
indicates time zone
can anyone tell me how to convert this type of time stamp to required format?
Upvotes: 1
Views: 156
Reputation: 5883
If you're on Java 8 and Groovy 2.5,
java.time.OffsetDateTime.parse('2018-07-18T01:40:05-0400', "yyyy-MM-dd'T'HH:mm:ssXX")
.format('MM/dd/yyyy hh:mm')
On Java 8 and an earlier version of Groovy,
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
OffsetDateTime.parse('2018-07-18T01:40:05-0400', DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXX"))
.format(DateTimeFormatter.ofPattern('MM/dd/yyyy hh:mm'))
Upvotes: 2
Reputation: 890
If i correctly understand problem You should prepare input timestamp and use Date.parse
.
def input = '2018-07-18T01:40:05-0400'
pdata = input.replace("T", " ").substring(0, input.replace("T", " ").length() - 5)
def out = Date.parse("yyyy-MM-dd hh:mm:ss", pdata).format("MM/dd/yyyy hh:mm")
/* Output
println "Input date: " + input
println "Parse date: " + out
Input date: 2018-07-18T01:40:05-0400
Parse date: 07/18/2018 01:40
*/
Upvotes: 1