Julian Day là gì?
Julian Day (ngày Julien) là số ngày liên tục kể từ ngày 1/1/4713 TCN. SQLite dùng julianday() để tính toán ngày giờ chính xác, hỗ trợ so sánh, cộng/trừ ngày.
DatabaseHelper.java - Truy vấn julianday
public ArrayList<String> getAllContacts() {
ArrayList<String> list = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
// julianday('now') trả về số ngày Julien hiện tại
Cursor cursor = db.rawQuery(
"SELECT id, name, salary, julianday('now') as jd " +
"FROM " + TABLE_NAME, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(0);
String name = cursor.getString(1);
String salary = new String(cursor.getBlob(2));
double jd = cursor.getDouble(3);
list.add(id + " : " + name + " : " + salary + " : JD=" + jd);
} while (cursor.moveToNext());
}
cursor.close();
return list;
}
Ứng dụng thực tế
- Tính khoảng cách ngày:
julianday('now') - julianday(created_at) - Lọc bản ghi gần đây:
WHERE julianday('now') - julianday(date_col) < 7(7 ngày qua) - Sắp xếp theo ngày:
ORDER BY julianday(date_col) DESC
Kết quả

Cột JD hiển thị số ngày Julien (dạng thập phân) tại thời điểm truy vấn.
Các hàm liên quan
| Hàm | Mô tả |
|---|---|
| date() | Trả về YYYY-MM-DD |
| time() | Trả về HH:MM:SS |
| datetime() | Trả về YYYY-MM-DD HH:MM:SS |
| julianday() | Số ngày Julien (float) |
| strftime() | Định dạng tùy chỉnh |