TNR*_*TNR 16
SQLite使用ISO8601日期/时间格式存储表示UTC(GMT)当前时间的字符串.这种格式(YYYY-MM-DD HH:MM:SS)适合于日期/时间比较.
使用以下代码检索日期.
Cursor row = databaseHelper.query(true, TABLE_NAME, new String[] {
COLUMN_INDEX}, ID_COLUMN_INDEX + "=" + rowId,
null, null, null, null, null);
String dateTime = row.getString(row.getColumnIndexOrThrow(COLUMN_INDEX));
Run Code Online (Sandbox Code Playgroud)
这样,返回一个字符串,解析它并重新格式化为您的本地格式和时区:
DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = iso8601Format.parse(dateTime);
} catch (ParseException e) {
Log.e(TAG, "Parsing ISO8601 datetime failed", e);
}
long when = date.getTime();
int flags = 0;
flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME;
flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE;
flags |= android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR;
String finalDateTime = android.text.format.DateUtils.formatDateTime(context,
when + TimeZone.getDefault().getOffset(when), flags);
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你.
Rod*_*Rod 13
SQLite实际上没有DATE类型(DATE关键字只是意味着列具有NUMERIC亲缘关系,根据SQLite版本3中的数据类型),因此您可以选择一个约定来存储日期.常见的约定是(a)使用实数来存储Julian日期或(b)使用整数来存储Unix纪元(自1970年以来的秒数,SQLite日期和时间函数支持每个日期和时间的'unixepoch'参数功能).
如果你正在做存储日期为Unix纪元(方便的Android,因为调用.getTime()一个对Date对象返回自1970年以来的毫秒数),然后阅读SQLite的DATE字段作为long,并通过毫秒相当于该进入java.util.Date构造Date(long milliseconds).所以,它看起来像这样:
SQLiteManager dbManager = new SQLiteManager(context, DB_NAME, null, VERSION);
SQLiteDatabase db = dbManager.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME,
new String[] { COLUMN_NAME_ID, COLUMN_NAME_DATE },
null, null, // selection, selectionArgs
null, null, null, null); // groupBy, having, orderBy, limit
try {
while(cursor.moveNext()) {
int id = cursor.getInt(0);
// Read the SQLite DATE as a long and construct a Java Date with it.
Date date = new Date(cursor.getLong(1)*1000);
// ...
}
} finally {
cursor.close();
db.close();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32285 次 |
| 最近记录: |