欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > 时间戳同日搜索

时间戳同日搜索

2025/5/23 23:31:39 来源:https://blog.csdn.net/sonichty/article/details/141567901  浏览:    关键词:时间戳同日搜索

关键方法

使用循环拼接SQL查询条件

已知问题

DatePicker隐藏年无效

 

builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_menu_search);
builder.setTitle("同日搜索");
DatePicker datePicker1 = new DatePicker(MainActivity.this);
datePicker1.setCalendarViewShown(false);
builder.setView(datePicker1);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@ Overridepublic void onClick(DialogInterface dialog, int which) {int month = datePicker1.getMonth();int day = datePicker1.getDayOfMonth();//隐藏年:https://blog.csdn.net/csdnadcode/article/details/38760743,无效//Field f[] = datePicker1.getClass().getDeclaredFields();//for (Field field : f) {//    if (field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner")) {//        field.setAccessible(true);//        Object yearPicker = new Object();//        try {//            yearPicker = field.get(datePicker1);//        } catch (IllegalAccessException e) {//            throw new RuntimeException(e);//        }//        ((View) yearPicker).setVisibility(View.GONE);//    }//}//隐藏年:https://www.jianshu.com/p/425d6e46db53,无效int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");if (daySpinnerId != 0) {View daySpinner = datePicker1.findViewById(daySpinnerId);if (daySpinner != null) {daySpinner.setVisibility(View.GONE);}}DBHelper helper = new DBHelper(MainActivity.this);Cursor cursor = helper.queryMonthDay(month, day);int count = cursor.getCount();setTitle(month + 1 + "月" + day + "日的事件" + count);adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.item_event, cursor, from, to, 0);adapter.setViewBinder(viewBinder);listView.setAdapter(adapter);}
});
builder.setNegativeButton("取消", null);
builder.create().show();

获取数据库最大、最小time并取年,年循环构建日期,time>= and time <=+1天,循环拼接并+or。

public Cursor queryMonthDay(int month, int day) {db = getWritableDatabase();Cursor cursor = db.rawQuery("SELECT MAX(time) FROM " + TableName, null);long time_max = -1;if (cursor.moveToFirst()) {time_max = cursor.getLong(0);}cursor.close();Date date = new Date(time_max);int year_max = date.getYear();cursor = db.rawQuery("SELECT MIN(time) FROM " + TableName, null);long time_min = -1;if (cursor.moveToFirst()) {time_min = cursor.getLong(0);}cursor.close();date = new Date(time_min);int year_min = date.getYear();String selection = "";for (int year = year_max; year > year_min; year--) {date = new Date(year, month, day);selection += "(time >= " + date.getTime() + " and time <= " + (date.getTime() + 24 * 60 * 60 * 1000) + ")";if (year > year_min + 1) {selection += " or ";}}Cursor c = db.query(TableName, null, selection, null, null, null, "time desc");return c;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词