Answer the question
In order to leave comments, you need to log in
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;
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question