chiranjib
chiranjib

Reputation: 5308

Insert TimeStamp in SQLite for Android

I have the following fields

1> WorkName - Varchar

2> TimeStap 

I wanted to create a Table with the above fields.

  1. What will be the TimeStamp datatype

  2. How can I insert timestamp values to the table.

  3. What will the timestamp value while inserting data or how to fetch the timestamp.

I have worked on SQLite but don't have any experience on adding TimeStamp as a field to the table & adding values to that.

What kind of CREATE & INSERT statements should I use?

Upvotes: 9

Views: 7689

Answers (3)

phongnt
phongnt

Reputation: 781

I create timestamp by this:

/**
 *
 * @return yyyy-MM-dd HH:mm:ss formate date as string
 */
public static String getCurrentTimeStamp(){
    try {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTimeStamp = dateFormat.format(new Date()); // Find todays date

        return currentTimeStamp;
    } catch (Exception e) {
        e.printStackTrace();

        return null;
    }
}

Upvotes: 2

Akash Agarwal
Akash Agarwal

Reputation: 2520

You could use Java.sql.Timestamp class as the column datatype of your table in a SQL database.

Since SQLiteDatabase class doesn't support a put() method for Timestamp variables, use the getTime() method in the class to insert the corresponding milliseconds of that time as a long value.(SQLiteDatabase does have a put() method for long type)
Open this for the method reference.

Upvotes: 0

John J Smith
John J Smith

Reputation: 11923

There are limited datatypes available in a Sqlite Database, so the one I find useful for dates is integer - this will accept long values (dynamically adjusts its size) so for dates store the milliseconds value of a date and the date can be easily reconsituted when reading the millisecond values back out of the database.

Upvotes: 7

Related Questions