I store date
field in the database as number of seconds since epoch:
For the timestamp in the image (1550591783
—representing 2019-02-19 19:26:23
), sqlite should return 50
as day of the year but it returns 40
.
This is the query in PurchaseDao
:
@Query("SELECT strftime('%j', date, 'unixepoch', 'localtime') AS day " +
"FROM purchase " +
"WHERE ...")
abstract List<Cost> getCosts();
and this is the date converter:
public class DateConverter {
@TypeConverter
public static Date fromTimestamp(Long value) {
return value == null ? null : new Date(value * 1_000); // multiply by 1000 to make it milliseconds
}
@TypeConverter
public static Long toTimestamp(Date date) {
return date == null ? null : date.getTime() / 1_000; // divide by 1000 to store as seconds
}
}
Even if I pass now
as the parameter to query (I've actually even applied it to a fresh method without any other distractions) I get the same wrong result:
@Query("SELECT strftime('%j', 'now', 'localtime')")
I've tried removing 'localtime'
argument, changing date converters to store date as string (for example in the format 2019-02-19
) and running the app in an AVD but I get the same wrong result in all cases.
On the other hand when I get day of the year with Calendar (Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
) or run the queries in my PC with stetho, the results are correct.
Any help appreciated.
1550591783
. And the thing is that when I run the query on the app database in my computer (with stetho) the result is correct. – Prestissimo