ユーザーがいくつかの文字列を保存するデータベーステーブルがあります。日付を含む 2 つの文字列の違いを、文字列である数値と比較したいと考えています。より具体的には、2 つの日付はこの形式に従います (例: "2012-10-24 00:00")。数値は "2" のようになります。クエリで 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;
}