Getting date difference in sqlite

前端 未结 2 1830
执念已碎
执念已碎 2021-01-14 11:03

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[] get         


        
相关标签:
2条回答
  • 2021-01-14 11:52

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

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

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

    (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();
        }
    }
    

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

    0 讨论(0)
  • 2021-01-14 12:00

    Hi please try these one

        Cursor  cursor = db.rawQuery("SELECT julianday('now') - julianday(DateCreated) FROM COUPON", null);
    
     if (cursor.moveToFirst()) {
                    do {
                       array[i]=cursor.getString(0)
                    } while (cursor.moveToNext());
    
                }
    
    0 讨论(0)
提交回复
热议问题