0

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.

4

1 回答 1

1

我有点困惑@CodeJoy。我在您的代码中没有看到任何对 +1 的引用。我假设您在为按钮文本构建 val 时正在执行 +1?

ORMLiteDate通过 Sqlite 驱动程序(即类似的东西2011-08-10 18:33:30.316)将该字段存储为字符串,我想知道与Calendar对象的转换是否会生成与数据库不完全Date匹配的。也许毫秒已被截断?您是否从按钮日期字符串创建一个?Calendar

您的问题很可能与本月的 +/- 1 问题无关。该getTime()方法应该适当地进行转换。

我将调试您的应用程序并查看cobj.getTime()日期的返回值,然后执行mDB.getSessionsDao().queryForAll()并查看如何Date从数据库驱动程序返回。

于 2011-08-10T22:37:51.790 回答