在sqlite中获取日期差异 [英] Getting date difference in sqlite

查看:69
本文介绍了在sqlite中获取日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get date difference between today and expiring day. This is the code I implemented. But this is not returning the right output.

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)).请帮助我在此处将日期差作为字符串获取到数组.

This returns (julianday('now') - julianday(EXPIRED_DATE)). Please help me to get date difference as string to a array here.

推荐答案

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

The now modifier returns not only the date but also the time.

要将时间戳更改为日期的开始,请使用 date()函数:

To change the timestamp to the start of the date, use the date() function:

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

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

(If the expired column also contains time values, you have to use date() for it, too.)

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

And to actually execute this, you have to give it to the database:

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 [] .)

(And the number of days is not a string; consider using int[] instead.)

这篇关于在sqlite中获取日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆