0

当我在我的数据库中插入一条记录时,我想输入它插入时的日期时间戳,这样我就可以对来自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挑选正确的日期时,从中插入的时间戳不匹配,为什么不呢?

4

1 回答 1

0

我建议在数据库表本身中添加日期时间的默认值。例如,在 MSSQL 中,您可以将默认值 datetime 设置为等于 GETDATE(),因此如果您添加新记录,数据库将自动为您插入当前日期时间。

于 2012-08-12T15:16:00.030 回答