java - 显示从数据库存储和读取的日期

标签 java android sqlite date

在我的应用程序中,一个重要的功能是将日期保存到 SQLite 数据库。将 Java 中的日期对象存储在数据库中的正确方法是什么?从数据库读取日期对象的最佳方法是什么?我是否必须使用 utilsql (来自命名空间)日期?

此外,该应用程序应在 Play 商店的所有国家/地区发布,因此我想在正确的位置字符串中显示日期。如果我想在 TextView 中显示一个字符串,如下所示:

Freitag, 06.03.2012      // Germany
Friday,  03.06.2012      // US (don't know if this is correct for US)

// The 0 befor one numbers is important for me

Android 中有没有一种方法可以在设置中设置的当前位置创建像上面这样的 DateString ?我想尽可能少地使用已弃用的方法。

最佳答案

来自SQLite Date and Time functions :

A time string can be in any of the following formats:

1)YYYY-MM-DD

因此您应该使用该格式在数据库中插入日期。 当你想在 android 中显示日期时,你可以查询数据库(获取格式为 YYYY-MM-DD 的行)。

之后,您可以按照您想要的方式设置该值的格式。例如,如果您想要Frid​​ay, 03.06.2012,您可以这样做:

try {
    String dateString = "2013-05-07";
    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
    Date date = inputFormat.parse(dateString);
    SimpleDateFormat outputFormat = new SimpleDateFormat("EEEE, dd.MM.yyyy", Locale.US);
    String result = outputFormat.format(date);
} catch (ParseException e) {
    e.printStackTrace();
}

现在,在结果中,您有星期二,07.05.2013。如果您将语言环境更改为 Locale.GERMAN,您将获得 Dienstag, 07.05.2013

关于java - 显示从数据库存储和读取的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16427781/

相关文章:

java - 我们期望 Ignite 何时触发 EVT_CACHE_ENTRY_CREATED 和 EVT_CACHE_OBJECT_PUT 事件

java - Spring Boot + React JS + Firebase 实时数据库

android - 进程无法访问该文件,因为该文件正在被另一个进程使用 [Android] [Gradle]

android - 在 RecyclerView 上设置最大高度

android - 带有文字说明的无限滚动画廊 View

android - SQLite 数据库,onUpgrade for version1 发布

sqlite - 一次拥有许多 SQLite 准备好的语句有缺点吗?

java - 从 JSONPath 数组的不同对象中查询具有特定值的键

java - final方法模拟

sqlite - Google Gears - 数据库 - VACUUM