【问题标题】:Get correct Date and Time (timezone, daylight) from SQLite Timestamp从 SQLite 时间戳获取正确的日期和时间(时区、日光)
【发布时间】:2014-05-23 05:04:56
【问题描述】:

好吧,我要疯了。我测试了许多解决方案,但没有正确的结果!

让我解释一下:我用 SQLite 数据库创建了一个应用程序。在一张桌子上,我记录了一些关于用户当前位置的信息,比如纬度、经度和时间戳!

我选择时间戳是因为它“与格式无关”。我稍后会在显示器上格式化它。

这是问题的开始。

我创建了一个将时间戳转换为日期和时间的字符串表示的函数。简单 ?是的,我之前也是这么想的。

这是函数(带有一些垃圾测试):

private String getDate(Timestamp timestamp) {
    /*Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
    cal.setTimeInMillis(timestamp.getTime());
    String date = DateFormat.format("dd/MM/yyyy HH:mm", cal).toString();*/

    //Time time = new Time(Time.getCurrentTimezone());
    /*Time time = new Time();
    time.set(timestamp.getTime());
    time.switchTimezone(Time.getCurrentTimezone());
    //String date = time.format3339(false);
    String date = time.format2445();*/

    //String date = DateFormat.getDateFormat(this).format(new Date(timestamp.getTime()));
    //String date = DateUtils.formatDateTime(this, timestamp.getTime(), DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);
    String date = DateFormat.getDateFormat(this).format(new Date(timestamp.getTime())) + " " + DateFormat.getTimeFormat(this).format(new Date(timestamp.getTime()));

    return date;
}

问题是显示的时间不对!例如,时间戳是今天 21:01,但它显示的内容类似于“09/04/2014 19:01”。两个小时的差异!

在我测试的每个解决方案中,差异都是相同的。为什么要两个小时?我猜是UTC时间。但我注意时区。

请务必注意,我拥有生成时间戳的位置。也许他可以用来确定正确的时区?

有什么建议或解决方案(即使不太容易)?

PS:我住在瑞士。

编辑:

创建条目时会自动创建时间戳 ('CREATION_TIMESTAMP' TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)。

这是我用来读取数据库位置的两个函数:

public Location getLocation(long id) {
    Cursor cursor = db.query(LocationSQLiteHelper.TABLE_NAME, allColumns, LocationSQLiteHelper.COLUMN_ID + " = " + id, null, null, null, null);

    cursor.moveToFirst();

    return cursorToLocation(cursor);
}

private Location cursorToLocation(Cursor cursor) {
    Location location = new Location();

    location.setId(cursor.getLong(0));
    location.setLatitude(cursor.getDouble(1));
    location.setLongitude(cursor.getDouble(2));
    location.setAccuracy(cursor.getFloat(3));
    location.setCreationTimestamp(Timestamp.valueOf(cursor.getString(4)));

    return location;
}

也许我应该改用日期?但这可能是格式的问题,不是吗?

【问题讨论】:

  • Sqlite 中的值是什么?
  • 你在哪里测试,模拟器?如果是,请检查模拟器的时间。
  • SQLite 时间戳不包含时区信息。保存和加载时间戳的功能必须一致地工作;给他们看。
  • @JonSkeet 在 SQLite 中,该值是时间戳。示例:1397060298000 表示 09/04/2014 20:18,但显示为 18:18!
  • @isah 不,它在真手机上,而不是模拟器上。

标签: android sqlite date timezone timestamp


【解决方案1】:

我找到了解决办法!事实上,时间戳是 UTC 并且没有存储有关时区的信息。

我的函数getDate()现在是这样的(https://stackoverflow.com/a/18692993/2416369建议的解决方案):

private String getDate(Timestamp timestamp) {
    Calendar calendar = Calendar.getInstance();
    TimeZone tz = TimeZone.getDefault();

    calendar.setTimeInMillis(timestamp.getTime());
    calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));

    SimpleDateFormat sdf = (SimpleDateFormat)SimpleDateFormat.getDateTimeInstance();
    Date currenTimeZone = (Date)calendar.getTime();
    String date = sdf.format(currenTimeZone);

    return date;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-26
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多