System.currentTimeMillis() returns incorrect timestamp on Huawei System.currentTimeMillis() returns incorrect timestamp on Huawei android android

System.currentTimeMillis() returns incorrect timestamp on Huawei


As a workaround for using System.currentTimeMillis(), maybe you can let sqlite handle the timestamp creation and see if that solves your problem?

Define/Alter your "created"-column with timestamp default current_timestamp or with default(strftime('%Y-%m-%d %H:%M:%f', 'now')) if you need milliseconds as well, like this:

sqlite> create table my_table(id integer primary key autoincrement not null, name text, created timestamp default(strftime('%Y-%m-%d %H:%M:%f', 'now')) not null);

sqlite> insert into my_table(name) values ('MyTestRow1');
sqlite> insert into my_table(name) values ('MyTestRow2');

sqlite> select * from my_table;

1|MyTestRow1|2017-08-07 10:08:50.898
2|MyTestRow2|2017-08-07 10:08:54.701


The java API call System.currentTimeMillis() in Android platform use the POSIX api gettimeofday to get the time in milli seconds. See here.

static jlong System_currentTimeMillis(JNIEnv*, jclass) {    timeval now;    gettimeofday(&now, NULL);    jlong when = now.tv_sec * 1000LL + now.tv_usec / 1000;    return when;}

It assume every call to gettimeofday will be successful. I guess your problem might happen here.

It's better to check the return value of every API call and determine what to do next if error happen.

So I suggest a more reliable method in JNI with your own implementation like below. Call these POSIX APIs in order, if gettimeofday failed, call clock_gettime, if it failed again, call time.

struct timeval now;if (gettimeofday(&now, NULL) != 0) {    struct timespec ts;    if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {        now.tv_sec = ts.tv_sec;        now.tv_usec = ts.tv_nsec / 1000LL;    } else {        now.tv_sec = time(NULL);        now.tv_usec = 0;    }}jlong when = now.tv_sec * 1000LL + now.tv_usec / 1000LL;__android_log_print(ANDROID_LOG_INFO, "TAG", "%lld", when);


When you don't have SIM card so no GSM/3G/4G, your phone can't update correct time based on the network provided time/zone.

enter image description here

So, devices with the network show the correct time, while other devices without the network can show incorrect time -- you have to manually set the correct time. System.currentTimeMilis() reads the time from your system. g But on power turn, the clock works.

Check if NTP (UDP port 123) is blocked by apps using Socket or DatagramSocket. Note: NTP applies to the scenario where clocks of all hosts or routers on the network must be the same. If your device switch to two (or more) different network and gets time updated from different sources, it can fluctuate time.

Ultimately, your system time is being changed, that's why it is fluctuating. If you manually System.currentTimeMilis() after you manually disable automatic date and time, I believe, it doesn't fluctuate (no anomaly). If this is the case then your Huewai tablet doesn't have bugs.