当前位置:首页 > 数据库 > SQlite

android – 在sqlite中获取日期差异

我想获得今天和到期日之间的日期差异.这是我实现的代码.但这并没有返回正确的输出.

public String[] getDaysList(){
    Cursor cursor = db.query("COUPON", null, null, null, null, null, null );
    if(cursor.getCount()<1){
        cursor.close();
        return null;
    }
    String[] array = new String[cursor.getCount()];
    int i=0;
    while(cursor.moveToNext()){

        String days = "(julianday('now') - julianday(EXPIRED_DATE))";
        array[i] = days;
        i++;
    }
    return array;
}

这将返回(julianday(‘now’) – julianday(EXPIRED_DATE)).请帮我在这里将日期差异作为字符串添加到数组中.

解决方法:

now修饰符不仅返回日期,还返回时间.

要将时间戳更改为日期的开头,请使用date() function

SELECT julianday(date('now')) - julianday(EXPIRED_DATE) FROM ...

(如果过期列也包含时间值,则必须使用date().)

要实际执行此操作,您必须将其提供给数据库:

public String[] getDaysList() {
    String days = "julianday(date('now')) - julianday("+EXPIRED_DATE+")";
    Cursor cursor = db.query("COUPON",
                             new String[]{ days },  // query returns one column
                             null, null, null, null, null);
    try {
        String[] array = new String[cursor.getCount()];
        int i = 0;
        while (cursor.moveToNext()) {
            array[i++] = cursor.getString(0);       // read this column
        }
        return array.length > 0 ? array : null;
    } finally {
        cursor.close();
    }
}

(并且天数不是字符串;请考虑使用int [].)


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!