2015-09-28 62 views
0

我保存它的提交的日期數據,但我的插圖更改爲當前日期日期在數據庫更改爲當前日期

當你保存數據

Calendar currentDate=Calendar.getInstance(); 
DatabaseOperations DB = new DatabaseOperations(ctx); 
DB.putInformation(DB, done_today1 + "\n" + done_today2 + "\n" + done_today3, thankful_for1 + "\n" + thankful_for2 + "\n" + thankful_for3 + "\n" + thankful_for4 + "\n" + thankful_for5, for_relationship, for_kids, for_business, currentDate.get(Calendar.DATE) + "-" + currentDate.get(Calendar.MONTH) + "-" + currentDate.get(Calendar.YEAR)); 

插入日期數據到表

public void putInformation(DatabaseOperations dop,String happenedToday,String thankfulFor,String forRelationship,String forKids,String forBusiness,String currentDate){ 
    SQLiteDatabase SQ=dop.getWritableDatabase(); 
    ContentValues cv=new ContentValues(); 
    cv.put(TableData.TableInfo.DONE_TODAY, happenedToday); 
    cv.put(TableData.TableInfo.THANKFUL_FOR,thankfulFor); 
    cv.put(TableData.TableInfo.FOR_RELATIONSHIP,forRelationship); 
    cv.put(TableData.TableInfo.FOR_KIDS,forKids); 
    cv.put(TableData.TableInfo.FOR_BUSINESS,forBusiness); 
    cv.put(TableData.TableInfo.CURRENT_DATE,currentDate); 
    SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv); 
    Log.d("Database operations", "One Row Inserted"); 

當我檢索日期這樣

Cursor CR = dop.getInformation(dop); 
CR.moveToFirst(); 
Toast.makeText(DisplayTable.this,""+CR.getString(5),Toast.LENGTH_LONG).show(); 

我得到的是當前日期,而不是數據提交日期。

任何人都知道爲什麼會發生這種情況?

回答

-1

處理日期和時間的最佳方法是始終使用ISO標準時間戳,即T和Z的時間戳。這使得在不同時區內實際日期的轉換變得輕鬆。

另一種方法是使用unix時間戳來保存日期和時間,它是一個可以轉換爲不同時區實際日期的長整數,所以它會根據您的時區始終反映正確的時間。

+0

謝謝,但它並沒有真正解決問題,您看到的問題是 ,問題是數據庫中的日期每天都會更改爲當前日期。例如,如果我今天提交了數據,日期將爲28-9-2015。 明天的日期應該是28-9-2015,而不是29-9-2015。 這是爲什麼發生? – Fisher

0

在SQL中,CURRENT_DATE是引用當前日期的關鍵字。

要具有相同名稱訪問列,你要引用的列名(雙引號標識符;單引號是字符串):

> CREATE TABLE t(current_date); 
> INSERT INTO t VALUES('x'); 
> SELECT current_date FROM t; 
2015-09-28 
> SELECT "current_date" FROM t; 
x 
> SELECT 'current_date' FROM t; 
current_date 

這可能是一個更好的主意使用不同的列名稱。

+0

是的,這是問題, 謝謝。 – Fisher