-1

I have made a database todo list application and would like to add the current time to it. However, I do not know how to do this in Java.

public void addProduct(ListItem product) {
  ContentValues values = new ContentValues();
  values.put(COLUMN_TODO, product.getToDo());
  values.put(COLUMN_FOR, product.getAccFor());
  SQLiteDatabase db = getWritableDatabase();
  db.insert(TABLE_LIST, null, values);
  db.close();
}

This is my current function to add an object to the database:

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productDB.db";
public static final String TABLE_LIST = "List";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TODO = "ToDo";
public static final String COLUMN_FOR = "accFor";
private String University = "University";
private String Work = "Work";
private String Personal = "Personal";

And these are the variables I declare at the start of the database, if it is possible using Android Studio I would like to also add the current time into the database so it can be displayed alongside the other values, GPS would be nice also, however, I feel this may be a little advanced.

public void onCreate(SQLiteDatabase db) {
  String query = "CREATE TABLE " + TABLE_LIST + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_TODO + " TEXT, " +
            COLUMN_FOR + " TEXT " +
            ");";
  db.execSQL(query);
}

This is the code for creation of the table.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

2 Answers2

0

You can call long currentTime = new Date().getTime and this is the current time in milliseconds (see documentation here). And then just add this long value to your database.

If you want to convert this time back to a date object, just call new Date(yourLongValue).

Cimoe
  • 580
  • 6
  • 21
0

Add a new column for time, then update it using System.currentTimeMillis().

  • public static final String COLUMN_TIME = "time" as a variable? or would it be int? values.put(COLUMN_TIME, System.currentTimeMillis()); and it would insert like this? – user7756528 Mar 23 '17 at 11:57
  • 1
    You need to update your database schema and add a new column for time with type "long" – Konstantin Buinak Mar 23 '17 at 12:11