Zach M.
Zach M.

Reputation: 1194

Android SQLite: Datetime between query

I am using the following code to store my date in my database:

String mydate = java.text.DateFormat.getDateTimeInstance().format(
                Calendar.getInstance().getTime());

This is a string in the db. There are multiple lists and I want to display to the user changes made based on the day.

In sqlserver there is an actual date variable and you can run BETWEEN on it, I am not sure how to implement this with the current datatype i'm using.

There may be a better way to store the date, but this was the only way that I have been able to successfully do it.

Upvotes: 1

Views: 665

Answers (2)

Simon Dorociak
Simon Dorociak

Reputation: 33515

This is a string in the db. There are multiple lists and I want to display to the user changes made based on the day.

Your actual code you can improve with defining custom Date pattern. You can use to achieve it for example SimpleDateFormat and then store Date as String with your defined pattern.

There are multiple lists and I want to display to the user changes made based on the day.

So first idea that came to my head is when you exactly know in which format you are storing your dates, you can simply use substr() function to get for example only day from a whole Date and then compare only days. Equivalent will be comparing only months, etc.


For tests if you specified correct pattern for SimpleDateFormat you can use:

Upvotes: 2

323go
323go

Reputation: 14274

If you're storing the date as a string, you'll need to specify a "comparable" format, such as YYYY-MM-DD HH:nn:ss, or, shorter, YYYYMMDDHHnnSS. You might be much better off storing the date as an integer epoch though (seconds since 1970). This is especially true if you're indexing the column and running comparative queries against it.

Upvotes: 1

Related Questions