You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过SQLiteOpenHelper存储时分数据至数据库(含时分格式示例)

用SQLiteOpenHelper存储时分数据的两种实用方案

嘿,这个问题我做Android项目时经常碰到,给你拆解成两种方案,按需选择就行👇

方案一:直接存储格式化后的字符串(直观易读)

这种方式最直接,把时分按HH:mm格式(比如12:40、00:00)存成TEXT类型,适合不需要做时间运算的场景。

1. 建表(SQLiteOpenHelper的onCreate方法)

在你的SQLiteOpenHelper子类里,创建表时把时间字段设为TEXT类型:

@Override
public void onCreate(SQLiteDatabase db) {
    // 创建带时分字段的表
    String createTableSql = "CREATE TABLE schedule (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "event_name TEXT," +
            "event_time TEXT)"; // 时分字段用TEXT类型
    db.execSQL(createTableSql);
}

2. 插入时分数据

插入前先把时间格式化成HH:mm的字符串,API 26+推荐用DateTimeFormatter(线程安全),低版本可以用SimpleDateFormat:

// API 26+写法
LocalTime localTime = LocalTime.of(12, 40); // 自定义时分
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
String timeStr = localTime.format(formatter);

// 插入数据库
ContentValues values = new ContentValues();
values.put("event_name", "午休");
values.put("event_time", timeStr);
db.insert("schedule", null, values);

3. 查询并显示时分

查询后直接取出字符串即可,不用额外转换:

Cursor cursor = db.query("schedule", new String[]{"event_time"}, null, null, null, null, null);
if (cursor.moveToFirst()) {
    do {
        String timeStr = cursor.getString(cursor.getColumnIndexOrThrow("event_time"));
        Log.d("TimeData", "时分:" + timeStr); // 直接输出12:40这类格式
    } while (cursor.moveToNext());
}
cursor.close();

方案二:存储整数分钟数(方便时间运算)

如果需要对时分做比较、加减(比如判断哪个时间更早),推荐存总分钟数(比如12:40就是12*60+40=760),用INTEGER类型存储,灵活性更高。

1. 建表(SQLiteOpenHelper的onCreate方法)

把时间字段设为INTEGER类型:

@Override
public void onCreate(SQLiteDatabase db) {
    String createTableSql = "CREATE TABLE schedule (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "event_name TEXT," +
            "event_minutes INTEGER)"; // 存储总分钟数
    db.execSQL(createTableSql);
}

2. 插入时分数据

把时分转成总分钟数插入:

// 比如要存12:40,计算总分钟数
int hours = 12;
int minutes = 40;
int totalMinutes = hours * 60 + minutes;

ContentValues values = new ContentValues();
values.put("event_name", "午休");
values.put("event_minutes", totalMinutes);
db.insert("schedule", null, values);

3. 查询并转成时分格式

取出整数后,再转换回HH:mm格式(注意补0保证两位数):

Cursor cursor = db.query("schedule", new String[]{"event_minutes"}, null, null, null, null, null);
if (cursor.moveToFirst()) {
    do {
        int totalMinutes = cursor.getInt(cursor.getColumnIndexOrThrow("event_minutes"));
        // 转成HH:mm格式
        int hours = totalMinutes / 60;
        int minutes = totalMinutes % 60;
        // 补0处理,比如00:05而不是0:5
        String timeStr = String.format(Locale.getDefault(), "%02d:%02d", hours, minutes);
        Log.d("TimeData", "时分:" + timeStr);
    } while (cursor.moveToNext());
}
cursor.close();

两种方案怎么选?

  • 要是只是展示时分,不需要做任何时间计算,选方案一,简单直接;
  • 要是需要排序、比较时间早晚、计算时间差,选方案二,整数运算比字符串方便太多。

内容的提问来源于stack exchange,提问作者ramtin m

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:45:13