I have sqlite database
private static final String DB_PROCESS_CREATE = "create table " + DB_TABLE_PROCESS + "(" + PROCESS_ID + " integer primary key autoincrement, " + PROCESS_DATE + " date);";
I create it db.execSQL(DB_PROCESS_CREATE);
How to add date value in this database? I tried :
String date = new SimpleDateFormat("yyyy.MM.dd").format(Calendar .getInstance().getTime()); ContentValues cv = new ContentValues(); cv.put(db.PROCESS_DATE,Date.valueOf(date)); db.mDB.insert(db.DB_TABLE_PROCESS, null, cv));
But then I get error
:
"The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Date)".
First, create a new table named datetime_real . Second, insert the “current” date and time value into the datetime_real table. We used the julianday() function to convert the current date and time to the Julian Day. Third, query data from the datetime_real table.
Date and Time Datatype. SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values: TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.
The date() function returns the date as text in this format: YYYY-MM-DD. The time() function returns the time as text in this format: HH:MM:SS. The datetime() function returns the date and time as text in their same formats: YYYY-MM-DD HH:MM:SS.
Now when you want to insert date to database, you can use this code.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String date = sdf.format(new Date());
In database insert the string 'date'
The date format in sqlite should be of following format:
YYYY-MM-DD YYYY-MM-DD HH:MM YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS.SSS YYYY-MM-DDTHH:MM YYYY-MM-DDTHH:MM:SS YYYY-MM-DDTHH:MM:SS.SSS HH:MM HH:MM:SS HH:MM:SS.SSS now DDDDDDDDDD
For more details, have a look: http://www.sqlite.org/lang_datefunc.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With