简体   繁体   English

如何检测客户端的时区?

[英]How to detect the timezone of a client?

如何在jsp中获取客户端/请求时区?

Unfortunately this information is not passed in HTTP headers.不幸的是,此信息未在 HTTP 标头中传递。

Usually you need cooperating JavaScript to fetch it for you.通常你需要配合 JavaScript 来为你获取它。
Web is full of examples, here is one http://www.coderanch.com/t/486127/JSP/java/Query-timezone网上到处都是例子,这里是一个http://www.coderanch.com/t/486127/JSP/java/Query-timezone

you cannot get timezone, but you can get current time from client side.ie through javascript and than post back.您无法获得时区,但您可以通过 javascript 从客户端获取当前时间,然后回传。 On server side, you can convert that time to GMT/UTC.在服务器端,您可以将该时间转换为 GMT/UTC。 The UTC shows the TimeZone. UTC 显示时区。

If you just need the local timezone in order to display local times to the user, I recommend representing all times in your service in UTC and rendering them in browsers as local times using Moment.js .如果您只需要本地时区以便向用户显示本地时间,我建议您使用 UTC 表示服务中的所有时间,并使用Moment.js在浏览器中将它们呈现为本地时间。

My general rule is to handle and store times in UTC everywhere except at the interface with the user, where you convert to/from local time.我的一般规则是在任何地方处理和存储 UTC 时间,除了在与用户的接口处,您在那里转换为/从本地时间。 The advantage of UTC is that you never have to worry about daylight-saving adjustments. UTC 的优势在于您永远不必担心夏令时调整。

Note that if you want to show the age of something (eg "posted 3 hours ago") you just need to compare the UTC timestamp with the current UTC time;请注意,如果您想显示某些内容的年龄(例如“3 小时前发布”),您只需将 UTC 时间戳与当前 UTC 时间进行比较; no need to convert to local times at all.根本不需要转换为当地时间。

Best solution for me is sending date/time as a string, and then parse with server's timezone to get a timestamp.对我来说最好的解决方案是将日期/时间作为字符串发送,然后用服务器的时区解析以获得时间戳。 Timestamps are always UTC (or supposed to be) so you will not need client's TimeZone.时间戳始终是 UTC(或应该是),因此您不需要客户端的时区。

For example, sending "10/07/2018 12:45" can be parsed like:例如,发送“10/07/2018 12:45”可以解析为:

SimpleDateFormat oD = new SimpleDateFormat();
oD.applyPattern("dd/MM/yyyy HH:mm");
oD.setTimeZone(TimeZone.getDefault()); // ;)
Date oDate = oD.parse(request.getParameter("time"));

Obviously you can set your specific date/time format.显然,您可以设置特定的日期/时间格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java API和客户端应用程序 - 如何正确处理时区 - Java API and client app - how to handle timezone properly 如何在不使用Cookie的情况下在Spring Controller中获取客户端时区偏移? - How get client timezone offset in spring controller without using cookie? 如何在servlet规范3中正确检测客户端断开连接? - How to properly detect a client disconnect in servlet spec 3? 如何在JSF应用程序中检测客户端区域设置? - How to detect client locale in JSF application? Jersey ChunkedOutput-如何检测客户端断开连接? - Jersey ChunkedOutput - how to detect client disconnections? 时区格式,如何知道时区 - Timezone format, how to know the timezone Java正则表达式以SimpleDateFormat模式检测时区 - Java regular expression to detect timezone in SimpleDateFormat pattern 如何从服务器(appengine)获取日期对象并将其转换为客户端的时区日期(gwt)? - How can I get a date object from server (appengine) and convert it to client's timezone's date (gwt)? 如何检测客户端何时与服务器断开连接 - How to detect when a client has disconnected from a server 如何在Java(Android)套接字通信中立即检测到服务器与客户端的断开连接 - How to detect the server disconnection to the client immediately in Java(Android) socket communication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM