在 android 的 SqlLite
中有 MyDate
行,如 DATETIME DEFAULT CURRENT_TIMESTAMP
。但是当我读取数据时,一个小时后数据又返回了。我在 android 环境中使用 GMT + 1
。
MyDate: 12:03
Android date: 13:03
当我从 SqlLite 读取数据时,根据 android 环境如何转换成 GMT + X
?
Cursor cursor = logsDao.getAll(LogsDao.KEY_ID + " DESC", "100");
String[] columns = new String[] { LogsDao.KEY_CONTENT, LogsDao.KEY_COMMENT, LogsDao.KEY_CREATED };
int[] to = new int[] { R.id.logContext, R.id.logComment, R.id.logCreated };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.log_entry, cursor, columns, to);
setListAdapter(mAdapter);
首先你应该在 Timestamp 中存储检索数据,然后再更新当前的时间。
String s = "2013-02-11 12:03:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UCT"));
Date timestamp = null;
try {
timestamp = df.parse(s);
df.setTimeZone(TimeZone.getDefault());
} catch (ParseException e) {
e.printStackTrace();
}
你需要实现一个自定义 cursorAdapter
@Override
public void bindView(View v, Context context, Cursor c) {
String date;
date= c.getString(c.getColumnIndex(LogsDao.KEY_CREATED));
//implement change of timezone
TextView date_text = (TextView) v.findViewById(R.id.date);
if (date_text != null) {
date_text.setText(date);
}
}