2013-02-25 74 views
0

日期的給定範圍我想通過以下方式獲取值檢索值:想從數據庫

我要取的總價值爲當前週數和我的代碼是在這裏:

SimpleDateFormat weekDateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
Calendar c = Calendar.getInstance(); 
c.set(Calendar.DAY_OF_WEEK, 1); 
Date weekFirstDate = c.getTime(); 
String strWeekFirstDay = weekDateFormat.format(weekFirstDate); 
Cursor weekCursor = database.rawQuery("select sum(cat_attribute_unit) from counter where cat_id ="+cat_id+" AND counter_entry_date BETWEEN '"+strWeekFirstDay+"' AND '"+todays_date+"';", null); 
int weekCounter = 0; 
if(weekCursor.moveToFirst()) 
    weekCounter = weekCursor.getInt(0); 
else 
    weekCounter = 0; 
weekCursor.close(); 

這是取當月的總數代碼:

SimpleDateFormat monthDateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
    Calendar c2 = Calendar.getInstance(); 
    c2.set(Calendar.DAY_OF_MONTH, 1); 
    Date monthFirstDate = c2.getTime(); 
    String strMonthFirstDay = monthDateFormat.format(monthFirstDate); 

    Cursor monthCursor = database.rawQuery("select sum(cat_attribute_unit) from counter where cat_id ="+cat_id+" AND counter_entry_date BETWEEN '"+strMonthFirstDay+"' AND '"+todays_date+"';", null); 
    int monthCounter = 0; 
    if(monthCursor.moveToFirst()) 
     monthCounter = monthCursor.getInt(0); 
    else 
     monthCounter = 0; 
    monthCursor.close() 

這是正確的?因爲它不正常工作,意味着我得到錯誤的價值觀。另外我想讓我的星期從星期一開始。有人會糾正我的代碼或建議一些新的代碼。

回答

1

存儲您所有的這種格式的日期,然後再試一次..

MM-dd-yyyy

Cursor monthCursor = database.rawQuery("select sum(cat_attribute_unit) from counter where cat_id ="+cat_id+" AND counter_entry_date BETWEEN '"+strMonthFirstDay+"' AND '"+todays_date+"';", null); 
+0

是它的工作。非常感謝。 – 2013-02-25 10:28:31

+0

它也使用格式「yyyy/MM/dd」。以前我保存的格式是「dd/MM/yyyy」。無論如何,感謝您的幫助,朋友.... – 2013-02-25 10:30:35