Let’s say I’m performing a SQL query from a Java program to get timestamps (stored as milliseconds) from a table of timestamps that occur within the last 10 days.
I can think of the following two ways to do this:
db.execSql("select * from timestamps where timestamp > (SELECT strftime('%s', 'now', '-10 days') * 1000)");
or
// First calculate in the number of milliseconds in Java
long t = System.currentTimeInMillis() - (10 * 86400000 /* millis in a day */);
db.execSql("select * from timestamps where timestamp > " + t);
Both get the job done and seem to be equivalent perf-wise when testing. Is one method preferred over the other? Does it matter? Is there an idiomatic way to do this?
I generally prefer to use the second method: if you end up needing to change the number of days, or the date from which the ten-day window is calculated, it will probably be more straightforward, understandable and maintainable to handle that in Java.