tl;dr
- Store date-only values as strings in standard ISO 8601 fashion: YYYY-MM-DD
- Query using ISO 8601 strings from the java.time classes, specifically
LocalDate
.
Example:
LocalDate ld = LocalDate.of( 2017 , Month.JANUARY , 23 ) ; // Or `LocalDate.parse`, or `LocalDate.now()`.
String sql = "DELETE * FROM tbl WHERE col >= '" + ld.toString() + "' ;" ;
Details
Keep in mind that SQLite was never meant to be a heavy-duty database, just a step up from writing text to a file. So if you need sophisticated date-time support, you should switch to a more sophisticated database.
SQLite lacks proper data types, as you alluded in the Question. For storing date-time types, the documentation suggests three routes: (a) ISO 8601 strings, (b) Real number representing Julian day numbers (I don't recommend this), and (c) Integer for storing number of seconds since the epoch reference of first moment of 1970 in UTC as shown in Answer by Abakumov (though you should NOT be using Date
class shown there).
You seem to be tracking date-only values, without a time of day. So I would use the ISO 8601 for text: YYYY-MM-DD.
The java.time types in Java use the standard ISO 8601 formats by default when parsing/generating date-time values. The date-only class in Java is LocalDate
, lacking a time-of-day and lacking a time zone.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate today = LocalDate.now( z ) ;
Specify a date.
LocalDate ld = LocalDate.parse( "2017-09-05" ) ; // Standard ISO 8601 format for input: YYYY-MM-DD.
The advantage of standard ISO 8601 format is that alphabetical order happens to also be chronological order.
So you can do text-based comparison using greater-than/less-than logic.
String sql = "DELETE * FROM tbl WHERE col >= '" + ld.toString() + "' ;" ;
Another big advantage over the other two approaches:
- Trying to represent date-time with a fractional real number is messy.
- Storing a count-from-epoch makes debugging and troubleshooting difficult as a human cannot readily read the meaning of the data.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?