1

Requirement : Export some data as a csv file when user selects export report as CSV in the browser.

Frontend - Angular 5 Backend - Springboot 1.5.10.RELEASE

We will be generating the report on the server and then sending it to the browser. The report has dates and they need to be shown in the timezone of the browser. Whats the best way to achieve this? Do I have to send the timezone as a URL request param?

humbleCoder
  • 583
  • 6
  • 22

1 Answers1

1

You can add a method argumentTimeZone to your controller, then obtain the timezone. Here is an example:

@RequestMapping
public String foo(..., TimeZone timezone, ...)
{
logger.info("This is the client timezone: " + timezone.getDisplayName());
}

According to Spring Documentation When available, the user’s Time-zone can be obtained by using the RequestContext.getTimeZone() method. As mentioned in comments LocaleContextResolver can be used for getting user time zone.

Note that browser default header doesn't have any client's time zone information. you have to add this information manually.

Amir
  • 634
  • 8
  • 18
  • Ok, from where does the framework get the timezone? Does the client has to send it explicitly or browser sends in the request and the framework injects here? – humbleCoder Nov 27 '18 at 11:51
  • @humbleCoder according to Spring framework documentation https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/support/RequestContextUtils.html#getLocale-javax.servlet.http.HttpServletRequest- Note: This method returns null if no specific time zone can be resolved for the given request. This is in contrast to getLocale(javax.servlet.http.HttpServletRequest) where there is always the request's accept-header locale to fall back to. – Amir Nov 28 '18 at 07:42
  • OK. Does this mean I have to explicitly pass the timezone information in the request. (An example would be helpful) unlike the accept-header locale which the browser adds automatically? – humbleCoder Nov 28 '18 at 08:47
  • Got this article explaining this. https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/client-time-zone.html – humbleCoder Nov 28 '18 at 09:44
  • It seems we are reading same article :), is there any more questions? – Amir Nov 28 '18 at 10:02