Forum Discussion

neerajan's avatar
neerajan
Occasional Contributor
8 years ago

Set the time zone to GMT of a formatted date (which is one of the request payload parameters)

I was trying to pass a formatted date value in the Media type of a request and I wanted to set the time to GMT. 

  • When I use the below syntax in the Media Type section of the request:

{
"recordId": "xxxxx",
"uploadDate": "${=new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").format(new java.util.Date())}",
"isActive": true
}

the "uploadDate" parameter is passed as -->  "Fri, 27 Oct 2017 12:30:21 CDT"

 

  • When I use the below syntax to try setting the time zone to GMT  instead of CDT

{

"recordId": "xxxxx",
"uploadDate":"${=new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").setTimeZone(new java.util.TimeZone.getTimeZone("GMT")).format(new java.util.Date())}",
"isActive": true
}

 

I get the below error

"uploadDate":"startup failed:
Script146.groovy: 1: unable to resolve class java.util.TimeZone.getTimeZone
@ line 1, column 75.
yyyy HH:mm:ss z").setTimeZone(new java.u
^
org.codehaus.groovy.syntax.SyntaxException: unable to resolve class java.util.TimeZone.getTimeZone
@ line 1, column 75.

 

Could you please help me resolve this issue?

 

  • In my testing expereinces, you cannot use java.util.Date and set a timezone. These objects do not contain any timezone information. I was able to get around this by instead using a calendar object. You can see an example below.

     

     

    ${= Calendar.getInstance(TimeZone.getTimeZone('GMT')).format("EEE, dd MMM yyyy HH:mm:ss z")}
  • groovyguy's avatar
    groovyguy
    Community Hero

    In my testing expereinces, you cannot use java.util.Date and set a timezone. These objects do not contain any timezone information. I was able to get around this by instead using a calendar object. You can see an example below.

     

     

    ${= Calendar.getInstance(TimeZone.getTimeZone('GMT')).format("EEE, dd MMM yyyy HH:mm:ss z")}
    • neerajan's avatar
      neerajan
      Occasional Contributor

      Awesome....that worked perfect :) !!