Forum Discussion

valentinamatera's avatar
valentinamatera
Occasional Contributor
4 years ago

Set date timezone in REST response

Hi, 

I need to set a different date timezone for my rest request response.

Do you know how I can do it?

 

Now, I have something like this in my REST Request Response when I run my (GET) REST Request :

<collectionDate>2021-04-11T06:35:56Z</collectionDate>

 

I need to have a date in Europe/Rome timezone so I need to set something somewhere to have a response like this:

<collectionDate>2021-04-11T08:35:56</collectionDate>

 

Do you know what I need to change?

Thanks in advance

  • nmrao's avatar
    nmrao
    4 years ago

    Here is the script which converts from one timezone to another.

    Uses java 8.

     

    import java.time.ZonedDateTime
    import java.time.ZoneOffset
    import java.time.format.DateTimeFormatter
    
    //Generic date pattern
    def dateFormat = "yyyy-MM-dd'T'HH:mm:ss[.SSS][xxx][xx][X]"
    
    /**
     * Closure returns DateTimeFormatter
     * based on the input pattern 
     * otherwise, ISO_OFFSET_DATE_TIME Date format
     */
    def getFormatter = { 
    	(null != it) ? DateTimeFormatter.ofPattern(it, Locale.ROOT) : DateTimeFormatter.ISO_OFFSET_DATE_TIME
    }
    
    /**
     * Converts date time string from one zone to another 
     * @sourceDateString - user date time string to convert
     * @offset - offset value of the target time zone 
     * @targetDatePattern - date pattern of the converted date time, default is "yyyy-MM-dd'T'HH:mm:ss[.SSS][xxx][xx][X]"
     * @sourceDatePattern - date pattern of the source date string, default is "yyyy-MM-dd'T'HH:mm:ss[.SSS][xxx][xx][X]"
     * @return formatted date time string with offset
     */
    def convertDate = { sourceDateString, offset, targetDatePattern = dateFormat, sourceDatePattern = dateFormat ->  
    	def sdate = ZonedDateTime.parse(sourceDateString, getFormatter(sourceDatePattern))
    	def tdate = sdate.withZoneSameInstant(ZoneOffset.of(offset))
    	tdate.format(getFormatter(targetDatePattern))
    }
    
    //Call
    log.info convertDate('2021-04-11T06:35:56Z', '+02:00', "yyyy-MM-dd'T'HH:mm:ss")
    

    The same is available at github

     

    Sample output

     

  • nmrao's avatar
    nmrao
    Champion Level 3

    Assuming that you need current date time in the given format mentioned in the question.

    Please try the below and see if that helps!

     

    <collectionDate>${= java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))}</collectionDate>

     

     

    • valentinamatera's avatar
      valentinamatera
      Occasional Contributor

      Thanks, but no I meant something different.

      I don’t have direct control on the date.

      I have a REST Request, a get REST request in which I don’t insert a date. I run it.

      The response of this REST request contain a date.

      I’d like to see or convert this date in my time zone (CEST).

      How I can do it?

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        Sorry, I overlooked the question, just realised.
        I thought it is for request.

        Do you need to convert the date received in response to particular timezone and assert it?

        What string you get in response and what is the expected after converting? An example would be great.