Date getMonth() in java from 0-11 messes up searches in database?
Second EDIT:
Looks like my issue might be where the date is set from the date picker dialog:
// 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();
}
}
I am +1 to the month, but never taking that off again when i compare for the database...
EDIT: Okay I did double checked this (finally got to it). Sure enough the
Date test1 = new Date(cobj.getTime().getTime()); //from the Calendar passed in
So the date retrieved from the database is the right date. The one that comes back from my dialog even though it displays correctly using:
String val = cobjstrong text.get(Calendar.MONTH) + "/" +
cobj.get(Calendar.DAY_OF_MONTH) + "/"+ cobj.get(Calendar.YEAR);
...is actually a month ahead when I look at the object as cobj.getTime().getTime(); (a long for the dates I use). Is there some other method or conversion I am missing?
Should I not be using the .getTime on the Calendar Object just to get a long from that (with a call to getTime again on the Date object?). Sometimes it seems to me that my best bet is to store longs in milliseconds to the database and then just retrieve them and do the Date conversions there?
PRE-EDIT question:
SO I have this Date field in a database, that I can store a date to and read a date from, when I read em... I have to add a +1 to the .getMonth() because date returns that as a number 0-11, instead of 1-12. After dealing with this issue and a few others (like .getMinutes returning an int, so if the time is 5:00 only 5:0 is displayed?)but I finally got the date displaying just great, but I found out when I try to query the database on a date things are off I am guessing by one month. So that means a month of
9/8/2011
(dd/mm/YYYY) format, will not query right when using the following ORMLite query:(Notice the .qe, GreaterThanEqual in 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) {
...
}
}
So I must be handling dates wrong, maybe adding to the month for display purposes is not right? or something... maybe in my query with the Calendar object, I can make that month part 0-11 or something...not sure what avenue to take here.
I'm a little confused @CodeJoy. I don't see any references to +1 in your code. I assume that you are doing a +1 while you are building the val for your button text?
ORMLite stores the Date
field as a string via the Sqlite driver (i.e. something like 2011-08-10 18:33:30.316
) and I am wondering if the conversion to/from a Calendar
object generates a Date
that does not match the database exactly. Maybe the milliseconds have been truncated? Are you creating a Calendar
from the button date string?
Most likely your problem has nothing to do with the +/- 1 issue around the month. The getTime()
method should do that conversion appropriately.
I would debug your app and see what the cobj.getTime()
returns for a date and then do a mDB.getSessionsDao().queryForAll()
and take a look at how the Date
is being returned from the database driver.
精彩评论