2017-09-12 125 views
0

如果當前日期和dealdate之間的差小於15分鐘,我必須運行服務。帶時間戳的ContentResolver查詢

我有創建表DBHelper類:

public class ToDoDBHelper extends SQLiteOpenHelper { 

private final int DATABASE_VERSION = 1; 
private ArrayList<ToDoClass> todoitems; 

private final String DATABASE_NAME = "deals.db"; 

public ToDoDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
    super(context, name, factory, version); 
} 


@Override 
public void onCreate(SQLiteDatabase sqLiteDatabase) { 

    final String Create_Table_Query = "CREATE TABLE "+ ToDoDatabase.ToDoDatabaseEntry.TABLE_NAME+" ("+ 
    ToDoDatabase.ToDoDatabaseEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+ 
      ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" TIMESTAMP ,"+ 
      ToDoDatabase.ToDoDatabaseEntry.end_dealDate_column+" TIMESTAMP ,"+ 
      ToDoDatabase.ToDoDatabaseEntry.shortdescription_column+" TEXT NOT NULL ,"+ 
      ToDoDatabase.ToDoDatabaseEntry.fulldescription_column+" TEXT NOT NULL "+");"; 
    sqLiteDatabase.execSQL(Create_Table_Query); 

} 

@Override 
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ToDoDatabase.ToDoDatabaseEntry.TABLE_NAME); 
    onCreate(sqLiteDatabase); 

} 

}

和我有ContentResolver的查詢在至極,我想使用的選擇標準:

Cursor cursor = context.getContentResolver().query(
      ToDoDatabase.ToDoDatabaseEntry.CONTENT_URI, 
      null, 
      selection, 
      null, 
      ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" DESC"); 

但我不知道如何編寫選擇。在選擇中,我需要區分ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column和currentDate。

請幫

回答

0

你可以試試這個

 long FIFTEEN_MINUTES = 15*60*1000; 
     String dateFormatSQLite = "yyyy-MM-dd HH:mm:ss"; //verify timestamp format in your case 
     String timezone = "GMT+0.00"; //use your timestamp 
     SimpleDateFormat sdf = new SimpleDateFormat(dateFormatSQLite); 
     sdf.setTimeZone(TimeZone.getTimeZone(timezone)); 
     String date = sdf.format(new Date(System.currentTimeMillis() - FIFTEEN_MINUTES)); 

如果你的情況的值存儲在時代在SQLite數據庫,那麼你可以做到這一點

 long FIFTEEN_MINUTES = 15*60*1000; 
     String date = System.currentTimeMillis() - FIFTEEN_MINUTES; 


     Cursor cursor = context.getContentResolver().query(
     ToDoDatabase.ToDoDatabaseEntry.CONTENT_URI, 
     null, 
     ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" <= ?", 
     new String[]{date}, 
     ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" DESC"); 
+0

謝謝你的回答。 –

+0

這對你有用@Renat Ahmetshin? –

+0

也許我必須將日期轉換爲時間戳: String date =「1410293471300」; String [] selectionArgs = new String [] {date}; –

0

@Shriyansh高塔姆感謝您的回答。 我找到了我的問題的解決方案。現在我正在使用SQLiteDatabase類的查詢方法。這是我的代碼,它適用於我。

Date curDate = new Date(); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(curDate); 
    calendar.set(Calendar.MINUTE, (calendar.get(Calendar.MINUTE)+15)); 


    String selection = ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" >= ?"; 
    String [] selectionArgs = {String.valueOf(calendar.getTimeInMillis())}; 

    String notifText = null; 
    ToDoDBHelper toDoDBHelper = new ToDoDBHelper(context,"deals", null, 1); 
    SQLiteDatabase sqLiteDatabase = toDoDBHelper.getWritableDatabase(); 

    Cursor cursor = sqLiteDatabase.query("deals", 
      null, 
      selection, 
      selectionArgs, 
      null, 
      null, 
      ToDoDatabase.ToDoDatabaseEntry.start_dealDate_column+" ASC");