按日期存储数据库记录
问题描述:
当我向我的数据库中插入一条记录时,我想将它插入的日期时间戳记,以便我可以从Date Picker
运行一个查询日期。这是将按日期存储数据库记录
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
cal.set(year, month, day);
long mDate = cal.getTimeInMillis();
mDate是什么被插入
,当我如何获得时间戳然后当我想运行在该日期项查询我这样做
public DateList(Calendar date){ //constructor
dt = date.getTimeInMillis();
}
装载机
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
return new CursorLoader(getActivity(),Games.GAMES_URI,new String[] {Games.GAMES_ID},Games.GAMES_DATE + "=" +dt ,
null,null);
}
但从date picker
的日期与插入的时间戳不匹配当选择正确的日期时,为什么不呢?
答
我会建议在数据库表中添加datetime的默认值。例如,在MSSQL中,您可以将默认值datetime设置为等于GETDATE(),因此如果添加新记录,数据库将自动为您插入当前日期时间。
我不明白你的意思,你可以举一个例子 – tyczj 2012-08-12 15:38:36