The question is as it sounds - how to retrieve the client user's time zone based on their browser input? If it matters, server side, J2EE is used - but I don't think that really matters in this case. Right now, HTTP is the protocol, it may use HTTPS when it goes live.
问题:
回答1:
Probably you could use some javascript: http://jeoftp.blogspot.com/2009/03/determine-java-timezone-of-your-web.html
回答2:
Seems somebody hacked some javascript to deduce this but yes, the best way is to let the user decide. But you can use the above link to choose the default for the user.
回答3:
I'm sure this is a duplicate.
The best way is to ask the user. Time Zone is not sent in the HTML headers, by default. Also consider that a user from one time zone may be visiting another. Which time zone should you use?
回答4:
You can also use IP geolocation services like ip2location. They sell a database subscription that will return the timezone in which the IP address is located. Of course, this has a price. A Javascript solution would work but does require that the client supports it.
The geolocation will support any client but can provide the wrong result depending on how the IP block was attributed or if the client is using a proxy server located in a different timezone (most likely an anomysing proxy service). The Javascript solution will support a large variety of client but will return the timezone base on what the computer is set to and not the timezone base on the actual location (traveling user don't always change the time on their computer). So there is no single solution that covers them all.
Using one of these two methods to make a guess and then allowing the user to correct it would be the best approach I would think. If you want to go overkill use both and let the user decide which makes most sense.
回答5:
As stated by Pierre-Luc Simard, which is a very good idea, you can use the IP to deduce that information. Credit goes to him but I'll add another link to a free alternative which is Geolite from Maxmind. There's also a sample code in PHP for the timezones and I'm pretty sure you'll find the equivalent in their Java API.