Set date timezone in REST response
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Labels:
-
REST
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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>
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, I have 2 alternative solution:
1. convert the date received in response to particular timezone and assert it
2. change some setting to force the REST Response to give me the date in a particular timezone
Now, I get in response this string:
<collectionDate>2021-04-11T06:35:56Z</collectionDate>
and the expected one is:
<collectionDate>2021-04-11T08:35:56</collectionDate>
Thanks
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
So are you getting UTC timezone by default in the response? Otherwise, do you know what is that zone?
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Regards,
Rao.
