Search code examples
spring-mvcgrailstimezone

Getting timezone information from request in Spring 4.0


There is a new feature of getting timezone information in web request object. See 16.9.1 section in http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/mvc.html#mvc-timezone

But I'm unable to resolve that how can we get the timezone information from request or which method should I use to get the timezone information?

After looking & following the source code at https://github.com/spring-projects/spring-framework/blob/v4.0.7.RELEASE/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java#L242, I tried to print it manually

println WebUtils.getSessionAttribute(request, SessionLocaleResolver.class.getName() + ".TIME_ZONE")

Any help?


Solution

  • Simply add a method argument of the type TimeZone to obtain it.

    @RequestMapping
    public String foo(TimeZone timezone) { ... }
    

    That should do it.

    If you really want to do it yourself use the RequestContextUtils.getTimeZone(request) method.

    @RequestMapping
    public String foo(HttpServletRequest request) {
        TimeZone timezone = RequestContextUtils.getTimeZone(request);
        ...
    }