2012-10-29 71 views
1

我有一個數據庫表,用戶在其中保存一些字符串。我想比較包含日期的兩個字符串與字符串的數字之間的差異。更具體地說:這兩個日期遵循這種格式,例如「2012-10-24 00:00」,數字就像這個「2」。有沒有辦法在查詢中把兩個日期區別開來?我也嘗試將日期格式化爲很長的日期,但由於通過整個數據庫的大循環,我得到了強制關閉。謝謝你提前與字符串的數據庫查詢,Android

日期從
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(); 
       } 



      } 

/轉換爲日曆/ 公共日曆DateToCalender(字符串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; 

     } 
+0

你的代碼在哪裏? –

+0

它不是必需的 – user1347280

回答

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); 

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

+2

我必須期望這樣的知名用戶的解釋。 –

+0

謝謝,但我真的需要你的解釋。第一列稱爲絕對日期(「2012-10-20 00:00),我希望與當前日期有所不同(」2012-10-17 00:00)「,結果將與number_of_days(」2 「)。 – user1347280