I have my application hosted in US. The server Time Zone is in GMT. Now when people accessed this application from across the Globe I want to get the Time zone for the Country/Regions from where it is accessed. Say - I'm accessing from some where in Europe. I want to get the time zone for that particular regions. I want this to be done in Java.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Two approaches you can take:
Approach 1 is to use javascript for this. I've used a module i found here with good results. I used code similar to this on a "jump page" that just picks up the timezone and then redirects on (passing the timezone as a parameter) in the login flow.
This is a lot easier than messing around with IP geolocation and since most people have their computer/device set to their "correct" timezone, it will be right most of the time.
I'm not entirely sure what you're actualy asking as I believe you could answer this question yourself.
You cannot get a timezone from the http request directly as mentioned here:
Is there another way to get a user's time zone from a HttpServletRequest object in Spring MVC?
But you can determine it by ip address. Mentioned here:
How to programmatically find geo location of incoming http request?
You can also use getTimeZoneOffset(). Mentioned here:
User needs to display transaction in there local time as they login from different country.
Prompting user for gmt or country code (and calculating gmt from that) in the registration still seems easier.
First we have to find the Time zone of a client.
Then as per the timezone we have to fetch time as follows:
You will need to map the clients IP address to a geo location first.
There are quite a few databases, that have this information.
For example, have a look at DB-IP, a database that has mapping information for IP addresses to various location information.
You can load the data to a local database and query it with the calles IP address. (You have this in your
HttpRequest
, when you are using Java Servlets.)The commercial version of the database ($90) has also the timezone in it.
Other possibilities include the use of a Web Service, like IP2Location. You would request the the data from the service, when a request is made by the client. Of course, this can be slow and you rely on the availability of the remote service.
There are other databases and services around, find the one that fits your needs.
I did not find one that is free and has the timezone information in it. A country information is surely not enough, when it comes to bigger countries.