在 Android 上以本地化格式显示日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2117565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying dates in localized format on Android
提问by Robert Petermeier
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:
我目前正在为 Android 构建我的第一个应用程序。我遇到的问题是与SimpleCursorAdapter
. 我有一个类封装了SQLitedatabase
对三个表的访问。此类负责以 ISO 格式(“yyyy-MM-dd”)存储所有日期。当从数据库中读取日期值并显示在屏幕上时,我希望它们以本地化格式进行格式化。这是我提出的方法。它使用 aViewBinder
进行格式化:
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
.
getDateFormatView()
创建一个SimpleDateFormat
具有从 读取的模式的对象strings.xml
。使用使用常量模式构造的 aparseDatabaseDate()
解析数据库中的日期。SimpleDateFormat
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.
- 两个
SimpleDateFormat
对象(一个用于parseDatabaseDate()
从 解析日期字符串SQLiteDatabase
,另一个用于格式化值) - 一个
java.util.Date
被创建然后立即扔掉的对象 - 相当多(在我看来)样板代码。
So that's why I'm asking: is there a better way to display dates in localized format?
所以这就是我问的原因:有没有更好的方法来以本地化格式显示日期?
回答by Dan Lew
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.
如果您想跳过解析,您可以将日期存储为 long。然后,您可以使用零解析的 long 创建一个新的 Date 对象。
This isn't directly related to your question, but: One thing you might want to consider using is android.text.format.DateFormatfor 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.
这与您的问题没有直接关系,但是:您可能要考虑使用的一件事是android.text.format.DateFormat用于获取日期格式化程序。通过这种方式,您可以将日期/时间格式化为用户的语言环境设置,而不是您自己的预设。它还可能使您的代码更简单,因为您不再需要创建自己的 SimpleDateFormat。
With these two things in mind, you can get rid of the calls to your own methods:
考虑到这两件事,您可以摆脱对自己方法的调用:
((TextView) view).setText(android.text.format.DateFormat.getDateFormat().format(new Date(cursor.getString(columnIndex))));