Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting datetime in sqlite database android

Tags:

android

sqlite

how can i insert datetime data in my sqlite database using contentvalues not using raw query?.

datetime('now') insert itself(text) not the time, and can i add addittional hours to the current time?

like, when i press button "1HOUR" it would insert the currenttime + 1 hour in the sqlite database..thanks, kinda confused..

like image 780
mcr619619 Avatar asked Oct 28 '12 01:10

mcr619619


People also ask

How to store datetime in SQLite?

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.


1 Answers

Convert date/time to milliseconds and you get a long. Then you just insert the long value in database.

You can add date/time values together if they are in milliseconds.

--EDITED--

    Date myDate = new Date();
    long timeMilliseconds = myDate.getTime();
    //add 1 hour
    timeMilliseconds = timeMilliseconds + 3600 * 1000; //3600 seconds * 1000 milliseconds
    //To convert back to Date
    Date myDateNew = new Date(timeMilliseconds);

In SQLite the java long value is stored as a int.

like image 85
Luis Avatar answered Oct 27 '22 00:10

Luis