还有另一种方法从Spring MVC中的HttpServletRequest对象获取用户的时区吗? [英] Is there another way to get a user's time zone from a HttpServletRequest object in Spring MVC?

查看:191
本文介绍了还有另一种方法从Spring MVC中的HttpServletRequest对象获取用户的时区吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将服务器时间转换为用户的时间,具体取决于他们的时区。

I need to convert my server time to the user's time depending on their time zone.

这是了解他们的时区的最佳方法 - 使用HttpServletRequest object?

Is this the best way to figure out their timezone - by using the HttpServletRequest object?

Locale clientLocale = request.getLocale();  
Calendar calendar = Calendar.getInstance(clientLocale);  
TimeZone clientTimeZone = calendar.getTimeZone();


推荐答案

很遗憾,您无法从他们的请求中获取用户的时区,因为客户端不发送数据。请求对象中的区域设置基于用户的Accept-Language标头。什么是英语的正确时区?

Unfortunately you cannot get the user's timezone from their request, as the client does not send the data. The locale in the request object is based on the user's Accept-Language header. What is the right timezone for "English"?

另外两种方法:


  • 在客户端的IP上使用GeoIP以获取其位置,并查找(从tz)关闭时区

  • 使用客户端Javascript计算其与UTC的偏移量(减去(来自localtime的UTC),然后查找与该偏移相匹配的时区(这不会非常精细,因为许多时区是例如+11小时。您可能能够与上述相结合。)

虽然没有真正好的简单解决方案 - 这就是为什么大多数网站会询问用户的时区,或以相对格式显示日期(例如5小时前)。

There's no real good simple solution to this though -- which is why most sites will ask for the user's timezone, or display dates in a relative format (e.g. 5 hours ago).

这篇关于还有另一种方法从Spring MVC中的HttpServletRequest对象获取用户的时区吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆