I am implementing an Android application in Java and I wanted to be able to validate the user's physical address information with the user's time zone. For example, the user can input Japan Standard Time, but the address could be in USA, which is obviously wrong. How would I validate this? I am using TimeZonePicker from AOSP to get the time zone details.
I know I could check the country of the time zone and the country listed by the user in the physical address, but some countries have multiple time zones so this wouldn't work in every case. It would probably be best I think to use city, state, and country since states like Tennessee have multiple time zones in the state.
Query the Google Time Zone API to return the state and timezone of the location provided.
If you are not using any of the location providing sensors, you can reverse geolocate the address to latitude and longitude coordinates.
Once you have the coordinates, query Google TimeZone API
https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510×tamp=1331161200&key=API_KEY
You will get obtain response with time zone name and timezone id:
{
"dstOffset" : 0.0,
"rawOffset" : -28800.0,
"status" : "OK",
"timeZoneId" : "America/Los_Angeles",
"timeZoneName" : "Pacific Standard Time"
}
Validation logic after this point is up to you.
Related
I need the real timezone ID but NOT of fake / generic timezone like Etc/GMT-xxx.
I understand that there are some solution to get the offset, or even the "fake" timezone, with the commands:
int offset = new Date().getTimezoneOffset();
String timezone = TimeZone.createTimeZone(offset).getID();
And currently our project is using this. However, this will only give you the summarized offset, but you will never know if this offset actually contains the DST (Daylight Saving Time / Summer Time) offset. Or in other words : you know the total offset is 2 hours, but it wont tell if its actually 2 + 0, or 1 + 1, and it really does matter in my use case.
Here is our use case : We have a function that allow user to upload excel file, where the function will parse the excel file and insert the records into system. Each of the record will have different Date Time. Our function have to check the below:
If the browser TimeZone (from where the user upload the file) subject to DST_offset.
If it set subject to DST, we then further decide if the Date is fall under "summer".
Our system then offset the date time and store into DB accordingly.
So, inside a batch excel file, the multiple records there some may have DST Offset while some may not.
I understand that we can use the java Calendar to achieve the above logic 1 & 2, from this link https://www.baeldung.com/java-daylight-savings. But however, the pre-requisite is : we need the real timezone ID e.g. "Europe/Rome", but not the fake or general one "Etc/GMT-xxx".
Please advise how could we get the "real" timezone ID in Smart GWT? or if any alternative to handle the mentioned use case?
I added ZoneId.systemDefault() a few days ago to https://github.com/adrianmsmith/gwt-time which hopefully should do exactly what you want. It returns the zone like “Europe/Rome”, from the browser, if the browser makes this info available (IE does not, for example). Feel free to reach out to me via my email address on my github account if you have any issues.
I have many users and they are located in different timezones. When a user changes some data (for instance edits) with her or his timezone, others must see it with their timezone. For this what can I do? Firstly , I need to save date with UTC timezone and when user wants to get it, It does request to api and gets returned date. After that user can convert this date with its timezone. Other users also do so. I think it works such
In short:
if you have to deal with daylight saving time (DST), you have to stick to java.time.ZonedDateTime
If you don't care about DST - use java.time.OffsetDateTime
In any case for all communications use ISO8601 standard
I have a date stored in a database as a UTC date ('2015-04-24 00:00:00'). When I get this date (using Hibernate) on a server running in the Eastern US Timezone, is shown as the correct date, '2015-04-24 00:00:00'. However, when I get this data on a server running on AWS in the us-west-1 region, which I assume is Pacific Time, the date shows as '2015-04-23 20:00:00'. Does anyone know why this would happen? I assume it has something to do with the date converting to local time, rather than UTC, but how can I prevent this from happening? Thanks!
Short answer: Just convert your Date to utc
Somewhat longer answer: As long as we don't consider relativistic effects a date (or more appropriately named a point in time) doesn't have a time zone. But if you display it as a String there are many variants of displaying it. One kind of variation is the choice of a timezone.
If you don't take special care to use a specific time zone, many programs will choose the timezone that the machine is 'living' in, which is configured on the os level and set to the time zone that is apropriate for the location the computer is most of the time.
Hi I am very new to Android Development. I want to pick the country name based on the current time of the particular mobile, while clicking the button. How can i do it? Any body tell me? Thanks in advance.
It seems difficult considering most timezones contain multiple countries (and countries multiple timezones as well).
See: Android: Is there a way to get timezone for Country name?
Per above, the closest you'll get comes from here:
For getting the time zone, check out the "O" and "T" format specifiers
of the date() function. "O" will give you the Difference to Greenwich
time (GMT) in hours (your time zone offset) and "T" will give you the
time zone abbreviation like "EST" for Eastern Standard Time.
e.g. <?php echo date("T"); ?> will give you the executing PHP script timezone, for the user timezome you can pass their date/time as the second argument.
I don't think thats possible because you can have MANY MANY countries on the same time. For example, just take the GMT+/-0 time zone, you'll have England, France, Spain, Portugal, and a few others you can get from the african countries. You can't do it like that.
What i'd suggest is to implement a GEOIP location using for example: MaxMind GEOIP. You have some very powerful tools available in PEAR for that and it took me about 4 hours to setup the library, understand the code and do the code to query. I was querying for IPs and countries and even states/provinces in less than 4 hours. You can't get faster than that unless your a genious :)
I'm trying to write a world clock app that returns the sunrise and sunset information for a given time zone. The thinking is that the user will either specify the GMT offset or give the time zone explicitly. Then the application will look up the latitude and longitude using the java.util.TimeZone found (or possibly given the GMT offset). I realize the time zones are usually not a very accurate way to get geolocation information since they are mostly based on political borders, but it seems like something like this exists. Most of the sites/apis I've find will give the timezone for the latitude and longitude, but I want to do a reverse search (return latitude and longitude for the given time zone). Thanks in advance for your help.
Let me show you the level of error you can expect if you do this to my home country, New Zealand, which is in one time zone (excepting remote islands), yet stretches over a large range of latitudes:
Invercargill, New Zealand
Time zone: NZST
Sunrise today: 7:05 am
Auckland, New Zealand
Time zone: NZST
Sunrise today: 6:34 am
Expect similar problems with China (the whole country is one timezone), Chile, Argentina, and others.
Nice idea, but probably unworkable.
Geonames.org has a downloadable database of towns that includes all kinds of information, including timezone and population. You could use it to populate a database that maps from timezone to the location of the largest city in that timezone.
Note that GMT offsets are very useless for this purpose, but fortunately the continent/city terminology of timezone IDs makes it very easy for users to choose the correct one.
Time zone is an area, so you would have to return coordinates of the border of this area (and probably add some method to check given point is inside). So you have to starts with geo data. Keep in mind that lot of countries have summer time so the time zones are not constant.
After reading everyone's answer, I think I'll probably use Google's Geocoding API, which returns the latitude/longitude and timezone for a given physical address. I'd just have to add an interface that lets the user enter an address, city or post code in order to retrieve the info from the api and calculate the sunrise/sunset times. This is better than trying to store all this information locally in a database (which would be huge). Thanks to all who answered.