在 Android 上以本地化格式显示日期
我目前正在构建我的第一个 Android 应用程序。我遇到的问题是与 SimpleCursorAdapter 相关的日期存储和本地化显示。我有一个类,它封装了对具有三个表的 SQLitedatabase
的访问。此类负责以 ISO 格式(“yyyy-MM-dd”)存储所有日期。当从数据库读取日期值并将其显示在屏幕上时,我希望将它们格式化为本地化格式。这是我想出的方法。它使用 ViewBinder
进行格式化:
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
if (view.getId() == R.id.text1) {
((TextView) view).setText(getDateFormatView().format(
parseDatabaseDate(cursor.getString(columnIndex))));
return true;
} else if (view.getId() == R.id.text2) {
((TextView)view).setText(
cursor.getString(columnIndex));
return true;
} else {
return false;
}
}
});
getDateFormatView()
使用从 strings.xml 读取的模式创建一个
。 SimpleDateFormat
对象parseDatabaseDate()
使用使用常量模式 yyyy-MM-dd
构造的 SimpleDateFormat
解析数据库中的日期。
虽然这段代码工作得很好,但我想知道是否有更好的方法来做到这一点。我不喜欢的是我需要:
- 两个
SimpleDateFormat
对象(其中一个用于parseDatabaseDate()
中,以便从SQLiteDatabase< 解析日期字符串/code>,另一个用于格式化值)
- 一个
java.util.Date
对象,该对象创建后立即丢弃 - 大量(在我看来)样板代码。
所以这就是我问的原因:是否有更好的方法以本地化格式显示日期?
I'm currently building my first app for Android. What I'm having trouble with is the storage and localized display of dates in connection with a SimpleCursorAdapter
. I have a class that encapsulates access to an SQLitedatabase
with three tables. This class takes care of storing all dates in ISO format ("yyyy-MM-dd"). When the date values are read from the database and displayed on the screen I want them to be formatted in a localized format. Here's the approach I've come up with. It uses a ViewBinder
to do the formatting:
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
if (view.getId() == R.id.text1) {
((TextView) view).setText(getDateFormatView().format(
parseDatabaseDate(cursor.getString(columnIndex))));
return true;
} else if (view.getId() == R.id.text2) {
((TextView)view).setText(
cursor.getString(columnIndex));
return true;
} else {
return false;
}
}
});
getDateFormatView()
creates a SimpleDateFormat
object with a pattern that is read from strings.xml
. parseDatabaseDate()
does the parsing of the date from the database with a SimpleDateFormat
that is constructed using a constant pattern yyyy-MM-dd
.
Although this code works just fine I'm wondering if there is a better way to do that. What I don't like is that I need:
- two
SimpleDateFormat
objects (one is used inparseDatabaseDate()
in order to parse the date string from theSQLiteDatabase
, the other is used to format the value) - a
java.util.Date
object that is created then thrown away immediately - quite a lot of (in my opinion) boilerplate code.
So that's why I'm asking: is there a better way to display dates in localized format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想跳过解析,可以将日期存储为 long。然后,您可以使用带有零解析的 long 创建一个新的 Date 对象。
这与您的问题没有直接关系,但是:您可能需要考虑使用的一件事是 android.text.format.DateFormat 用于获取日期格式化程序。通过这种方式,您可以将日期/时间格式化为用户的区域设置,而不是您自己的预设。它还可能使您的代码更简单,因为您不再需要创建自己的 SimpleDateFormat。
记住这两件事,您可以摆脱对您自己的方法的调用:
If you want to skip on the parsing, you could store the date as a long instead. Then you could just create a new Date object using the long with zero parsing.
This isn't directly related to your question, but: One thing you might want to consider using is android.text.format.DateFormat for getting your date formatters. This way you format your dates/times to the user's locale settings instead of your own presets. It might also make your code simpler, as you don't need to create your own SimpleDateFormat anymore.
With these two things in mind, you can get rid of the calls to your own methods: