2017-07-03 95 views
0

我從在「MM/DD/YYY」的形式與服務器的日期,然後我把它轉換成使用下面的函數毫秒:Android SQlite:按日期範圍向後查詢表格行?

public static long getSimpleDateToMillis(String simpleDate) throws ParseException { 
     SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); 
     Date date = formatter.parse(simpleDate); 
     return date.getTime(); 
} 

然後後來我將結果保存到數據庫中INT。

現在我陷入了對我來說似乎是死衚衕的地步。我無法通過搜索和從我的股票知識中找到一種方法,即如何通過在數據庫中保存爲整數的project_date列過濾我的cursorloader。

以什麼方式可以查詢,以便: 從項目表中選擇project_date爲今天和向後(昨天等)的所有行。

我試過這個,但似乎真的不是答案。

String [] projection = new String []{}; 
     String selection = "datetime("+ ProjectsEntry.COLUMN_PROJECT_DATE + "/1000, 'unixepoch') =? "; 
     String [] selectionArgs = new String[]{" date('now')"}; 
     return new CursorLoader(this, 
       JobsContract.JobsEntry.CONTENT_URI, 
       projection, 
       selection, 
       selectionArgs, 
       null); 

我還沒有找到任何其他的參考資料可以指向我,所以我希望有人也許也會遇到這個問題。

+0

「datetime(」+ ProjectsEntry.COLUMN_PROJECT_DATE +「/ 1000,'unixepoch')''<''=?你試過」小於或等於「? – Grisgram

+0

@Grisgram不起作用。 –

回答

0

這是我如何做一些非常相似的事情,但使用完整的時間步長即long而不是int。

首先,我有這樣的方法來獲取時間戳,讓今天的日期/時間爲午夜(巴1毫秒)的: -

/** 
* 
* @return 1 millsecond before midnight today 
*/ 
private long getDateTimeOfAllofToday() { 
    Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.DAY_OF_MONTH,1); // tomorrow 
    cal.set(Calendar.MILLISECOND,0); 
    cal.set(Calendar.SECOND,0); 
    cal.set(Calendar.MINUTE,0); 
    cal.set(Calendar.HOUR_OF_DAY,0); 
    cal.add(Calendar.MILLISECOND,-1); 
    return cal.getTimeInMillis(); 
} 

然後,我創建了相應的where子句例如: -

filter = DBRulesTableConstants.RULES_ACTON_COL + 
     " <= " + 
     Long.toString(getDateTimeOfAllofToday()); 

這是通過使用rawQuery所以不是正是你想要什麼,但足夠容易改變" <= "" <=?"然後用 String [] selectionArgs = new String[]{Long.toString(getDateTimeOfAllofToday())};或修改版本,以獲得整數。