SQLite CursorLoader选择参数 - 日期格式列

问题描述 投票:0回答:1

在我的Android应用程序中,我有一个数据库,它将日期作为System.currentTimeInMilis的长值存储。我现在想使用CursorLoader查询特定日期。这是我尝试过的:

DateFormat sameDayCheckerformatter = new SimpleDateFormat("dd-MM-yyyy");

String selection = sameDayCheckerformatter.format(ItemEntry.COLUMN_DAY) + "=" + sameDayCheckerformatter.format(dayInMilis);

return new CursorLoader(getActivity(),
            ItemContract.ItemEntry.CONTENT_URI_ITEMS,
            projection,
            selection,
            null,
            null);

这不像我预期的那样有效:

java.lang.IllegalArgumentException:无法将给定的Object格式化为Date

但我找不到解决方案。我该如何解决这个问题?

android sqlite android-cursorloader
1个回答
1
投票

为什么要将格式应用于ItemEntry.COLUMN_DAY? 它看起来像一个代表列名的String。 您还需要用引号括起格式化的日期。 改为:

String selection = ItemEntry.COLUMN_DAY + " = '" + sameDayCheckerformatter.format(dayInMilis) + "'";

编辑:如果列ItemEntry.COLUMN_DAY具有整数值,那么您根本不需要格式化:

String selection = ItemEntry.COLUMN_DAY + " = "  + dayInMilis;

编辑2:你需要strftime()功能:

String selection = 
    "strftime('%d-%m-%Y', " + ItemEntry.COLUMN_DAY + " / 1000, 'unixepoch') = '" + 
    sameDayCheckerformatter.format(dayInMilis) + "'";
© www.soinside.com 2019 - 2024. All rights reserved.