第二個編輯:java中的getMonth()函數在0-11中數據庫中的搜索?
看起來像我的問題可能是其中的日期從日期選取器對話框中設置:
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear + 1;
mDay = dayOfMonth;
updateDisplay();
}
}
我+1到一個月,但從來沒有采取這一關時,我再次比較數據庫...
編輯: 好吧,我做了雙重檢查(終於到了它)。果然,
Date test1 = new Date(cobj.getTime().getTime()); //from the Calendar passed in
所以從數據庫中檢索的日期是正確的日期。從我的對話框回來的一個,儘管它顯示正確使用:
String val = cobjstrong text.get(Calendar.MONTH) + "/" +
cobj.get(Calendar.DAY_OF_MONTH) + "/"+ cobj.get(Calendar.YEAR);
...實際上是提前一個月的時候我看對象cobj.getTime()的getTime()。 (我使用的日期很長)。有沒有其他方法或轉換我缺少?
我是不是應該在Calendar對象上使用.getTime來獲取長時間(再次調用Date對象的getTime?)。有時候我認爲最好的選擇是將毫秒數的數據存儲到數據庫中,然後檢索它們並在那裏進行日期轉換?
PRE-EDIT問題:
,所以我有這個日期字段在數據庫中,我可以存儲日期和讀取的日期,當我讀到嗯...我要補充一個+1到.getMonth()因爲日期返回數字0-11,而不是1-12。在處理了這個問題和其他一些問題之後(比如.getMinutes返回一個int,所以如果時間是5:00,只顯示5:0),但是我終於看到日期顯示很好,但是當我嘗試時在一個日期查詢數據庫的東西關閉我猜測了一個月。因此,這意味着
2011/9/8
(日/月/年)格式的一個月,使用以下ORMLite查詢時不會查詢權利:(注意.qe,GreaterThanEqual在ormlite)。
public void updateDatePickerButtonUI(Calendar cobj, int widget) {
String val = cobj.get(Calendar.MONTH) + "/"+ cobj.get(Calendar.DAY_OF_MONTH)
+ "/"+ cobj.get(Calendar.YEAR);
btnChooseDateReview.setText(val);
//the following just won't query correctly, its a month off
try {
//sessionDate
QueryBuilder<SessionsData, Integer> sb =
mDB.getSessionsDao().queryBuilder();
sb.where().ge(SessionsData.SESSIONSDATE_ID_NAME, cobj.getTime());
List<SessionsData> sessions = mDB.getSessionsDao().query(sb.prepare());
for (int i = 0; i < sessions.size(); i++) {
try {
mDB.getClientsDao().refresh(sessions.get(i).getClient());
mDB.getPackagesDao().refresh(sessions.get(i).getPackage());
} catch (SQLException e) {
...
}
}
theSessions = new CustomSessionReviewAdapter(mContext,
R.layout.session_review_row, sessions);
theSessions.notifyDataSetChanged();
theList.setAdapter(theSessions);
} catch (SQLException e) {
...
}
}
所以我必須處理日期錯誤,也許增加到本月顯示的目的是不正確的?或者什麼......也許在我的日曆對象的查詢中,我可以使那一個月的零件0-11或其他...不知道要採取什麼途徑。
是的正確的假設所有的顯示是加一...但好計劃我會嘗試這些建議,謝謝 – Codejoy
好吧,我仔細檢查了這個(終於到了它)。果然,Date test1 = new Date(cobj.getTime()。getTime());會產生比我選擇的月份晚一個月的日期...... – Codejoy
我真的懷疑這是'Calendar'對象的問題。難道是你正在做+1去按鈕,但是當你回到'Calendar'對象然後將它存儲在數據庫中時不會做-1? – Gray