Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert current date in sqlite from android application

I want to insert current date in SQLite database datetime type field. How can I do this?

UPDATE : Using mDb.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (datetime()) ");

I am able to insert like date_created=2012-10-10 13:02:08 and I want this format 10 Oct 2012 12:48 how to achive this?

like image 502
Ankit HTech Avatar asked Jun 20 '26 08:06

Ankit HTech


1 Answers

I prefer the following ways to get it done:

  1. Create the table with a default settings to put current datetime automatically. For example:

    ColumnName DATETIME DEFAULT CURRENT_TIMESTAMP
    
  2. Create SimpleDateFormat and convert current date into SQL format string. For example:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strDate = sdf.format(new Date());
    
    ContentValues values = new ContentValues();
    values.put("ColumnName", strDate);
    
like image 67
waqaslam Avatar answered Jun 22 '26 20:06

waqaslam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!