1

我有一个数据库表,用户在其中保存了一些字符串。我想比较两个包含日期的字符串和一个字符串之间的差异。更具体地说:两个日期遵循这种格式,例如“2012-10-24 00:00”,数字就像这样“2”。有没有办法在查询中获取两个日期之间的差异?我也尝试将日期格式化为长,但由于整个数据库的大循环,我被迫关闭。先感谢您

mCursor1.moveToFirst();
                mCursor1= mDbHelper.fetchTasks();

                     for(int u=0; u<mCursor2.getCount(); u++){

                   mCursor1.moveToPosition(u);
                  long id=mCursor1.getLong(0);


                Date Date1 = ConvertToDate(this.mCursor1.getString(5));
                 Date DateNow = ConvertToDate( current );
                 Calendar a= DateToCalender (this.mCursor1.getString(5));
                 Calendar b= DateToCalender(current);
                 a.add(Calendar.MONTH, 1);
                 b.add(Calendar.MONTH, 1);



                           long diff= daysBetween(b,a);
                            if(dateNotify=="1"&& diff%1==0){
                                 mDbHelper.updateNotification(id,"true");

                                    }
                                 else if(dateNotify=="2"&& diff%2==0){
                                      mDbHelper.updateNotification(id,"true");

                                      }
                                 else if(dateNotify=="3"&& diff%3==0){
                                         mDbHelper.updateNotification(id,"true");

                                      }
                                 else      if(dateNotify=="7"&& diff%7==0){
                                         mDbHelper.updateNotification(id,"true");

                                      }



              mCursor1.moveToNext();
                 }



          }

/从日期转换为日历/ public Calendar DateToCalender(String str_date){

              SimpleDateFormat  formatter ; 
              Date date ; 
              Calendar cal;
              formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
              try {
                 date = (Date)formatter.parse(str_date);
                 cal=Calendar.getInstance();
                 cal.setTime(date);
                 } catch (ParseException e) {
                 e.printStackTrace();
                return null;
            } 
          return cal;
         }




        /*Days difference between two dates*/
        public static long daysBetween(Calendar startDate, Calendar endDate) {
              Calendar date = (Calendar) startDate.clone();
              long daysBetween = 0;
              while (date.before(endDate)) {
              date.add(Calendar.DAY_OF_MONTH, 1);
              daysBetween++;
               }
              return daysBetween;
         }


        /*Convert string to Date*/
       public Date ConvertToDate(String dateString){

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

                Date convertedDate;
                try {

                    convertedDate = dateFormat.parse(dateString);


                } catch (ParseException e) {
                    e.printStackTrace();
                    return null;
                }

                return convertedDate;

         }
4

1 回答 1

0

试试这个查询。

return db.query(TABLE_NAME, new String[]{"temp_sp_date","temp_sp_amount"},
                        "(temp_sp_user_id = ?) and (temp_sp_date >= ? and temp_sp_date <= ? )" , new String[] {String.valueOf(userId),startDate,endDate}, " strftime(" + "\"%Y" + "-" + "%m" + "-" + "%d\""  + ",temp_sp_date) ",null , null);

根据您的要求更改字段和值。

于 2012-10-29T12:20:03.510 回答