Java Code Examples for org.springframework.format.datetime.standard.DateTimeContext

Following code examples demonstrate how to use org.springframework.format.datetime.standard.DateTimeContextfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.format.datetime.standard.DateTimeContextand various code implementation of this class.

    protected void setParameterHolder(ZoneId timeZone) {
        DateTimeContext context = new DateTimeContext();
        context.setTimeZone(timeZone);
        DateTimeContextHolder.setDateTimeContext(context);
    } 


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		DateTimeContext context = new DateTimeContext();
		context.setTimeZone(getZoneId(request));
		DateTimeContextHolder.setDateTimeContext(context);
		return true;
	} 

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
        super.preHandle(request, response, handler);
        DateTimeContextHolder.setDateTimeContext(new DateTimeContext());
        String newTimeZone = request.getParameter(getTimeZoneParamName());
        if (newTimeZone != null) {
            LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
            if (localeResolver == null) {
                throw new IllegalStateException(
                        "No LocaleResolver found: not in a DispatcherServlet request?");
            }

            if (localeResolver instanceof LocaleContextResolver) {
                LocaleContext localeContext = new SimpleTimeZoneAwareLocaleContext(localeResolver.resolveLocale(request), TimeZone.getTimeZone(newTimeZone));
                ((LocaleContextResolver) localeResolver).setLocaleContext(request, response, localeContext);
            }
        }


        return true;
    } 

Advertisement
Javadoc
A context that holds user-specific java.time (JSR-310) settings such as the user's Chronology (calen

dar system) and time zone. A null property value indicate the user has not specified a setting. @author Juergen Hoeller @since 4.0 @see DateTimeContextHolder

Read More
Advertisement