我有一個數據庫表,用戶在其中保存一些字符串。我想比較包含日期的兩個字符串與字符串的數字之間的差異。更具體地說:這兩個日期遵循這種格式,例如「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;
}
你的代碼在哪裏? –
它不是必需的 – user1347280