Storing dates in formats that are recognised by SQLite (see Date and Time Functions) can be quite beneficial.
- They are directly sortable
- They can be manipulated within queries (see example)
- They can take up less storage and be more efficient
YYYY-MM-DDThh:mm:ss is such a format, although storage wise storing them as an integer (Long), which is recognised format can be marginally more efficient.
The T is an accepted separator (as per ISO 8601) between the date and time, there is no need to change it to a space (it would not hurt to change it to a space).
Example
Here's an example that stores the date in YYYY-MM-DDThh:mm:ss, and automatically converts the extracted date in the Order to a Date object.
After adding 3 rows, it extracts them, it then runs an update to add 7 days to all orders and extracts the modified data.
A version of the Order Entity (columns omitted for brevity) :-
@Entity
class Order {
@PrimaryKey
var orderId: Long? = 0L
var date: String? = ""
@Ignore
var dateAsDate: Date? = null /* @Ignore so this is not a column in the table */
/* Constructor that converts the date to a Date */
constructor(orderId: Long?, date: String) {
this.orderId = orderId
this.date = date
this.dateAsDate = SimpleDateFormat(DATE_FORMAT_IN_DATABASE).parse(date)
}
companion object {
const val DATE_FORMAT_IN_DATABASE = "yyyy-MM-dd'T'HH:mm:ss"
const val SQLITE_STRFTIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
}
}
Dao :-
@Dao
interface Dao {
@Insert
fun insert(order: Order): Long
@Query("SELECT * FROM `order`")
fun getAllOrders(): List<Order>
@Query("UPDATE `order` SET date = strftime('${Order.SQLITE_STRFTIME_FORMAT}',date,'+7 days')")
fun addAWeekToAllOrderDates()
@Query("DELETE FROM `order`")
fun deleteAllOrders()
}
- see Link above regarding the SQLite
strftime
function
The @Database is nothing special so omitted for brevity
An activity putting it all together that:-
- gets an instance of the database
- gets the Dao from the database
- deletes any existing rows
- adds 3 rows
- extracts the 3 rows and prints the extracted date as per the database and the date as per the Date's
toString
method.
- updates all the rows by adding 7 days using SQLite's
strftime
function.
- extracts the 3 updated rows and prints the extracted date as per the database and the date as per the Date's
toString
method
:-
class MainActivity : AppCompatActivity() {
lateinit var db: TheDatabase
lateinit var dao: Dao
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
db = TheDatabase.getInstance(this)!!
dao = db.getDao()
dao.deleteAllOrders()
dao.insert(Order(null,"2021-01-01T12:15:00"))
dao.insert(Order(null,"2021-02-01T12:15:00"))
dao.insert(Order(null,"2021-03-01T12:15:00"))
for(o: Order in dao.getAllOrders()) {
Log.d("ORDERINFO","Extracted date is ${o.date} from Date is ${o.dateAsDate.toString()}")
}
dao.addAWeekToAllOrderDates()
for(o: Order in dao.getAllOrders()) {
Log.d("ORDERINFO","Extracted date is ${o.date} from Date is ${o.dateAsDate.toString()}")
}
}
}
Result (as output to the log) :-
2021-06-15 19:47:30.065 D/ORDERINFO: Extracted date is 2021-01-01T12:15:00 from Date is Fri Jan 01 12:15:00 GMT+11:00 2021
2021-06-15 19:47:30.066 D/ORDERINFO: Extracted date is 2021-02-01T12:15:00 from Date is Mon Feb 01 12:15:00 GMT+11:00 2021
2021-06-15 19:47:30.066 D/ORDERINFO: Extracted date is 2021-03-01T12:15:00 from Date is Mon Mar 01 12:15:00 GMT+11:00 2021
2021-06-15 19:47:30.073 D/ORDERINFO: Extracted date is 2021-01-08T12:15:00 from Date is Fri Jan 08 12:15:00 GMT+11:00 2021
2021-06-15 19:47:30.074 D/ORDERINFO: Extracted date is 2021-02-08T12:15:00 from Date is Mon Feb 08 12:15:00 GMT+11:00 2021
2021-06-15 19:47:30.074 D/ORDERINFO: Extracted date is 2021-03-08T12:15:00 from Date is Mon Mar 08 12:15:00 GMT+11:00 2021