The following code for now()
is executed twice for an object. Once to denote it's creation and another time to denote when it was written to the database.
In a controlled test I managed to reliably reproduce a bug where the dates vary widly between each other. Some dates seem to have the right date but are at 00:00:00
or nearly 00:00:00
.
Others seem to be relatively off by several hours varying from 1 hour to 16 or even weeks. I also had times set in the future. Calendar.getInstance.getTime()
should be equivalent to System.getCurrentTimeMillis()
, having the latter one return a future time once in a while is worry some.
I haven't found anything and going to investigate the issue further and report my findings here.
private static DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
public static String calendarToDb(Calendar cal) {
if (cal == null) {
return null;
}
return simpleDateFormat.format(cal.getTime());
}
public static String now() {
return calendarToDb(Calendar.getInstance());
}
Here is a the relevant part of a log from the android-monitor. This is a real device, not an emulator. M is for MessageManager and P is for a ContentProvider
08-30 18:07:17.267 M: main starttime = 2016-01-01T00:07:00+0100\
08-30 18:07:17.306 P: AsyncQueryWorker CreateTime = 2016-08-30T18:07:17+0200\
08-30 18:07:18.326 M: main starttime = 2016-01-01T00:00:00+0200\
08-30 18:07:18.371 P: AsyncQueryWorker CreateTime = 2016-08-30T18:07:18+0200\
08-30 18:07:19.898 M: main starttime = 2016-08-30T18:07:19+0200\
08-30 18:07:19.920 P: AsyncQueryWorker CreateTime = 2016-08-30T00:00:00+0100\
I have got more data in my database that looks even weirder and shouldn't happen at all.