D
D
Dmitry Kinash2015-04-03 15:19:56
Java
Dmitry Kinash, 2015-04-03 15:19:56

Why does java.util.Date depend on time zone?

General situation. The mobile app collects some data and periodically sends it to the server. The data is generated in the form of objects that are written to the database. The procedure from the minute timer selects the data with a request, restores the original objects and, if the data exists, it is sent to the server. Data can be generated both during the user's interactive work in an Activity, and in the background within a running Service, which processes some data automatically.
Technical details.One of the fields of the object that describes the event is of type java.util.Date - the date the event occurred. In the database, it is stored as a field of type INTEGER (although, as we know, this is a convention in SQLite). When writing to the database, I use the getTime () method from the date. When restoring from a database, I use the constructor with this time (UNIX epoch microseconds) parameter. Next, when sending data in JSON format to the server, I get getTime () and divide the result by a thousand (to get normal UNIX time).
The problem is this: Sometimes the messages leave with the correct time, and sometimes they are offset into the future by the time zone value.
I have never experienced this when testing, but sometimes it happens to users. Yesterday we were able to see and follow the incident in hot pursuit. The user triggered an event in17:57 , but saw on the site 20:57 . After that, he informed me (it was 18 with a change and therefore the problem was 100% confirmed). The user on the smartphone had the correct time and the correct time zone. In the server logs (in raw data) I found an entry for 17:57:30 with the following information {"Date": 1427997440 ..._other properties_...}, which is not difficult to verify corresponds to 02 Apr 2015 17:57:20 GMT.Thus, it was established that the incorrect time came from the mobile application, and did not result from a processing failure on the server. Unfortunately, after a successful connection to the server, the records of transmitted events are deleted from the database, and therefore it is no longer possible to specify at which stage the problem occurred - before or after saving to the database.
Two options:
either at 17:57:20 construction (new Date()).getTime() == 1427997440000 (instead of 1427986640000)
or (new Date(1427986640000)).getTime() == 1427997440000
What are the ideas of respected Android experts and experienced Java programmers?
PS Perhaps this will be important.There are thoughts that there is a problem with delayed optimization of the virtual machine. Since in the same project I came across other inexplicable things. For example, here is a working code (simplified):

int intCount = 0;
Cursor cursor = db.rawQuery(SELECT count(*) as count FROM table, null);
      if (cursor.getCount() > 0) {
cursor.moveToFirst();
...............
intCount = cursor.getInt(0));
...............
      }
cursor.close();
...............
object.field = intCount;

So this code is often launched and there are usually no questions to it. And now the owners of one of the devices began to complain about the fall of the program. I'm looking at the error message on Google Play - and there, in the last line of this example (assigning an object property to an int local variable), an error is raised that it is impossible to access the data of a closed cursor. Added all operations with a variable before closing the cursor and the error ceased to occur.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
constv, 2015-04-03
@constv

the user may have a zone set that does not correspond to the current time corresponding to this zone.
for example, according to the belt, it should be 15:00, and on the device, the user manually sets 16:00. and Date returns the time according to the set zone.

M
Marat S, 2015-04-05
@aratj

because, it's better to drive the time in UTC so that it doesn't matter what the time is on the device.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question