Giới thiệu
SQLite lưu trữ thời gian dưới dạng chuỗi TEXT, số REAL (Julian day), hoặc INTEGER (Unix timestamp). Hàm unixepoch modifier chuyển đổi Unix timestamp (giây từ 1970-01-01) thành ngày giờ dễ đọc.
Bước 1 & 2: Tạo dự án và Giao diện
Giao diện: EditText tên, lương, các nút CRUD, ListView.
Bước 3: MainActivity.java
Code tương tự các bài SQLite trước.
Bước 4: DatabaseHelper.java - Sử dụng unixepoch
package com.example.andy.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "salaryDatabase10";
public static final String CONTACTS_TABLE_NAME = "SalaryDetails";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + CONTACTS_TABLE_NAME +
"(id INTEGER PRIMARY KEY, name TEXT, salary REAL, datetime DEFAULT CURRENT_TIMESTAMP)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CONTACTS_TABLE_NAME);
onCreate(db);
}
public boolean insert(String name, String salary) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("salary", salary);
db.replace(CONTACTS_TABLE_NAME, null, cv);
return true;
}
// QUAN TRỌNG: Chuyển đổi timestamp sang ngày giờ địa phương
public ArrayList<String> getAllContacts() {
SQLiteDatabase db = getReadableDatabase();
ArrayList<String> array_list = new ArrayList<>();
// strftime('%s', 'now') = Unix timestamp hiện tại
// unixepoch = chuyển từ Unix time sang UTC
// localtime = chuyển UTC sang múi giờ địa phương
String query = "SELECT (id || ' : ' || TRIM(name) || ' : ' || salary || ' : ' || " +
"datetime(strftime('%s', 'now'), 'unixepoch', 'localtime')) AS fullname " +
"FROM " + CONTACTS_TABLE_NAME;
Cursor res = db.rawQuery(query, null);
res.moveToFirst();
while (!res.isAfterLast()) {
if (res != null && res.getCount() > 0) {
array_list.add(res.getString(res.getColumnIndex("fullname")));
}
res.moveToNext();
}
res.close();
return array_list;
}
public boolean update(String name, String salary) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("salary", salary);
return db.update(CONTACTS_TABLE_NAME, cv, "name = ?", new String[]{name}) > 0;
}
public boolean delete() {
getWritableDatabase().execSQL("DELETE FROM " + CONTACTS_TABLE_NAME);
return true;
}
}
Giải thích datetime modifiers
| Modifier | Chức năng |
|---|---|
unixepoch | Giá trị trước đó là Unix timestamp (giây), chuyển sang UTC |
localtime | Chuyển từ UTC sang múi giờ địa phương |
utc | Chuyển sang UTC (ngược lại localtime) |
+N hours | Cộng N giờ |
-N days | Trừ N ngày |
Ví dụ thực tế
-- Lưu timestamp hiện tại
INSERT INTO events (name, timestamp) VALUES ('Event', strftime('%s', 'now'));
-- Đọc lại thành ngày giờ địa phương
SELECT datetime(timestamp, 'unixepoch', 'localtime') FROM events;
Chạy ứng dụng

ListView hiển thị bản kèm ngày giờ hiện tại theo múi giờ địa phương.