在我的应用程序中,一个重要的功能是将日期保存到 SQLite 数据库。将 Java 中的日期对象存储在数据库中的正确方法是什么?从数据库读取日期对象的最佳方法是什么?我是否必须使用 util 或 sql (来自命名空间)日期?
此外,该应用程序应在 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 的行)。
之后,您可以按照您想要的方式设置该值的格式。例如,如果您想要Friday, 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/