/ / Uložiť a načítať a zobraziť aktuálny dátum a čas v databáze sqlite - android, sqlite, datetime

Ukladať a načítať a zobrazovať aktuálny dátum a čas v databáze sqlite - android, sqlite, datetime

Môj názov hovorí všetko, ako najlepšie môžem ukladať aktuálny dátum a čas v SQLite databáze a načítať a zobraziť spôsob, akým chcem ako 15:45, 20. januára 2014 v android?

odpovede:

1 pre odpoveď č. 1

Použite objekt kalendára a uložte ho ako dlhý.

Calendar cal = Calendar.getInstance(); // returns Calendar object set to current moment

// store in database: use your own Database class constructor, not "Database(context)"
SQLiteDatabase db = new Database(context).getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("my date column", cal.getTimeInMillis());
db.insert("my table", null, cv);
db.close();

// to retrieve date after using the cursor to get values from database:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cursor.getLong(cursor.getColumnIndexOrThrow("my date column"))); //resets the calendar object to the time stored from database

// display time in log
Log.d("The time is: ", cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) + " " + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.YEAR));
// the +1 on the month field is because January starts at 0.