2016-12-23 11 views
-1

では、次の列(すべての整数)Sqliteをクエリが(今の間で今、+ 30日)本日から30日間の行を、取得する

taskId | day | month | year 
------------------------ 
1  | 4 | 4  | 2016 
1  | 3 | 5  | 2016 
1  | 12 | 5  | 2016 
1  | 30 | 5  | 2016 

は日付が今であると仮定して、テーブルを持っていると仮定4/4/2016 Sqliteクエリでレコードを取得する方法。現在と今後30日の間です。 次の30日間のタスクを意味します。

ので、結果がどうあるべき、

1  | 4 | 4  | 2016 << today is 4/4/2016 
1  | 3 | 5  | 2016 

おかげ

答えて

2

これらの列をSQLiteのデフォルトの日付形式に変換するには、u SE printf()

printf("%04d-%02d-%02d", year, month, day) 

その後、組み込みの日付関数を使用することができます。

SELECT * 
FROM MyTable 
WHERE printf(...) BETWEEN date('now') AND date('now', '+30 days'); 
1

まず

私はdateでの作業はメーリングリストのよりよいだっただろうことを言わなければならない、あるいはまたそのdate.getTimeでの作業longを返します。

はJava側から

は、あなたがこのような何かをしなければならないだろうと述べた。

Caendar c = Calendar.getIstance(); 

int currentDay = c.get(Calendar.DAY_OF_MONTH); 
int currentMonth = c.get(Calendar.MONTH) + 1; //it's 0 based 
int currentYear = c.get(Calendar.YEAR); 

c.add(Calendar.DAY_OF_MONTH, 30); 
int nextMonthDay = c.get(Calendar.DAY_OF_MONTH); 
int nextMonthMonth = c.get(Calendar.MONTH) + 1; //it's 0 based 
int nextMonthYear = c.get(Calendar.YEAR); 

String myQuery = String.Format("select * from myTable where (day >= {0} and month >= {1} and year >= {2}) and (day < {3} and month <= {4} and year <= {5})", currentDay, currentMonth, currentYear, nextMonthDay, nextMonthMonth, nextMonthYear); 

これは日付が> =現在の日付とあるすべての行がかかります< =来月です

希望するもの

+0

これは私が従うつもり最良の方法です。ありがとう – Hatim

関連する問題