问题描述
我目前正在建设我的第一个Android的应用程序。什么我有麻烦的是存储和日期的使用 SimpleCursorAdapter
连接本地化的显示。我有一个封装了访问一个 SQLitedatabase
三用表一类。这个类需要存储的所有日期的ISO格式(YYYY-MM-DD)的照顾。当从数据库中读出并显示在屏幕上的时间值我希望他们在一个本地化的格式进行格式化。这是我想出来的方法。它采用了 ViewBinder
做的格式:
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()
创建了一个从串读的模式的
。 的SimpleDateFormat
对象的.xml parseDatabaseDate()
确实从与数据库的日期解析一个的SimpleDateFormat
正在使用恒定模式 YYYY-MM-DD 。
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
.
虽然这code工作得很好,我不知道是否有更好的方法来做到这一点。我不喜欢的是,我需要的:
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:
- 在两个
的SimpleDateFormat
对象(一个用于parseDatabaseDate()
,以便从解析日期字符串SQLiteDatabase
,另一种是用于格式化值) - 在
java.util.Date
对象,该对象被创建,然后扔掉,立即 - 在相当多的(在我看来)样板code。
- 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?
推荐答案
如果你想跳过的分析,你可以在日期存储作为一项长期的,而不是。然后用长着零解析你可以只创建一个新的Date对象。
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.
这是不直接关系到你的问题,而是:你可能要考虑使用的一件事是android.text.format.DateFormat让你的日期格式化。这样,你格式化你的日期/时间,以用户的区域设置,而不是你自己的preset中。它也可能使你的code简单,因为你不需要再创建自己的SimpleDateFormat。
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:
((TextView) view).setText(android.text.format.DateFormat.getDateFormat().format(new Date(cursor.getString(columnIndex))));
这篇关于显示日期在Android本地化的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!