dates change between client and server side (GWT) - java

I have a problem that I can't understand. I tried to find the answer a lot of time, but without success.
I work with GWT client side, and Java server side.
Client side, I read dates (java.util.date). And when I send these dates to server side, there sometimes is an hour offset when I receive it.
I know there are many problems with TimeZone. But I think TimeZone aren't responsible of my problem, because not all dates are wrong. To test which dates were wrong, I create a method which create a List of all dates between 1st January of 1900 and today, and which send this list to the server.
When I read the list received in server, here are the results :
All dates are correct from the year 1995 (dates didn't change during sending)
From 1979 to 1995 (approximately): All dates are correct, except 28 days in september / october (from the swith to winter daylight saving time). It's incorrect because of an offset of one hour.
Before : some dates are correct, and some incorrect.
So I tried to add 100 years to my dates client side, send it and remove 100 years server side. And all received dates were correct !
Anybody already had this problem ? And anybody understood this problem ? Any help is welcome.
Thanks !
Edit :
Ok I solved the problem. Read answer of Andrei Volgin to understand the problem. And here is the code which solved it :
// Create date you want
Date date = new Date()
// Get TimeZone of your date
TimeZone timeZone = TimeZone.createTimeZone(date.getTimezoneOffset());
// Adapt your date with the TimeZone
date.setTime(date.getTime() - (timeZone.getOffset(date) * 60000));
// You can send your date to server
// TimeZone server side is "UTC", and all dates received are correct

This is a TimeZone problem.
TimeZone definitions and especially daylight savings rules have changed over the years. If you simply pass the time zone ID or create a time zone using an offset, the browser is unaware of these changes. So the browser simply uses the time-zone offset and current DST setting for this time zone when displaying time. Most of the time this results in a correct time, but not always. This also explains why all dates in the future are correct.
In order to get the accurate conversion, you need to create a TimeZone object using a JSON data string that GWT provides, i.e. use createTimeZone(java.lang.String tzJSON) or createTimeZone(TimeZoneInfo timezoneData).

Related

Timezone from 3rd party devices

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

Hibernate date issue on servers in different timezones

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.

Getting difference of 1 day when date is sent from android application to php

I am developing the android application in which persons can enter leave application.
Here users select the date from the Android Calendar and from this selected Date Epoch value is sent to php page on converting the epoch readable date format i get a difference of 1 Day.
As on php page i calculate the to date for the leave using the number of leave and from date but here i m getting the wrong value as from date on the php differs from the selected date on the android application.
Kindly help..
It turned out to be a timezone issue. The dates get a default timezone upon creation. But it makes a difference between 0 and 1 day if there different timezones involved. Especially if you create a date, transfer the timestamp (as long int) and create another date from it.
For more details see the comments to the question :-)

Dates and time zones (javascript, java, struts)

I am sure this is a very very common problem.
I have a struts action that uses a common jquery datepicker calendar to select a date for my code to use. Problem is that our production servers live in Texas on CST, but I am in DC on EST. My web browser sends 12/6/11 12:00 AM EST (midnight) and it is interpreted as 12/5/11 11:00 PM CST by the server.
Any recommended ways to fix this?
Should I send the local browser's timezone as a parameter to my web services code?
If possible, always deal with UTC times internally in your code (and especially in communications between components). Convert to local time at the last possible moment before display, where you know what the user's time zone is.
In your case, convert the timestamp to UTC in the browser before sending to the server.
If you're doing something like setting appointment times in the future, beware of Daylight Saving Time, where everything gets shifted by an hour twice a year (but UTC doesn't). For example, the user will expect that setting an appointment for "next Tuesday at 2:00 pm" will work even if next Tuesday is a different UTC offset from now.

How do i get date from database other than GMT format?

Trying to get a time stored in a datadase.
select dbtimezone from dual gives me -07:00
I am using Java program to get the Date from Oracle
Column i type of Date.
while i am fetching the time in my java program am getting it as GMT.
actually i want the time as it is there in database not converted time.
Though i can convert back to -07:00 , i am seeking another way to do because conversion always depends on the dbtimezone of the database using.
Can any one help me ?
Thanks in advance
That oracle just has one timezone can make life difficult if you deal with different timezones. I've always thought life was easier if you consider timezone a view artifact and
represent all times as UTC, then convert in the view. You put the timezone information someplace in the database and convert accordingly.
...actually getting that right can get interesting because you don't want to make the same mistake of being too general again. For example, a client may be based in a particular timezone, but have offices in many. Though an office is in a particular timezone, the activity relating to the time may involve a different timezone etc.
See java.util.TimeZone, more specifically the getOffset methods, which return the number of milliseconds to add to the UTC time to get local time. Note that it also considers the daylight saving time.

Categories