在我的Android應用程序中,我試圖根據日期從我的數據庫檢索字符串。但我不希望在這一年的日子(這樣我只需要有365行)。我得到它的唯一方法是在我的數據庫中,使日期列中的日期格式爲yyyyMMdd。如果我將日期列中的所有日期更改爲MMdd格式,則兩位數月份(如Oct,Nov,Dec)可以工作,但是像Jan或Sept.(01,09)這樣的單位數月份會使應用程序崩潰與錯誤CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
。我已經在數據庫中嘗試過MM-dd,yyyy-MM-dd格式,並且它們會崩潰上面列出的錯誤應用程序。是否可以在Sqlite數據庫中僅使用MMdd格式化日期?有任何想法嗎?Android SQlite沒有一年的日期?
MainActivity:
DBT = new SimpleDateFormat("MMdd", Locale.US);
tMMddStr = DBT.format(new Date(System.currentTimeMillis()));
private void getTH() {
dba.open();
tArray = dba.getTHA(tMMddStr);
dba.close();
optionB_TV.setText(todayArray[2]);
hideDayAtv.setText(todayArray[1]);
hideYearAtv.setText(todayArray[0]);
}
將對DBAdapter:
public String[] getTHA(String tMMddStr) {
String[] columns = new String[] { KEY_Y, KEY_MD, KEY_HA };
Cursor cursor = db.query(DB_TABLE, columns, KEY_DATE + "=" + tMMddStr,
null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
String hapT[] = new String[3];
hapT[0] = cursor.getString(0);
hapT[1] = cursor.getString(1);
hapT[2] = cursor.getString(2);
cursor.close();
this.db.close();
return hapT;
}
return null;
}
您說得對!我將格式更改爲Mdd並且似乎可行。我想這是我沒有嘗試過的那個組合。 – kirktoon1882