jeudi 8 octobre 2015

Android: Substract two Datetime from SQLite [duplicate]

This question already has an answer here:

I Have data in database in SQLite like this.

ID   COLUMN_SBEGIN         COLUMN_SEND
1    2015-10-08 22:00:00   2015-10-09 08:00:00

I want to get how many HOURS from 22:00:00 to 08:00:00. Do you have any idea?

Here my code!

public void sleepProcess() throws ParseException {
    cursor = getContentResolver().query(ProviderSleep.CONTENT_URI, Helper.ALL_SLEEP,null,null,null,null);
    cursor.moveToFirst();
    String temp = cursor.getString(cursor.getColumnIndex(Helper.COLUMN_SBEGIN)); //get the time data
    String temp2 = cursor.getString(cursor.getColumnIndex(Helper.COLUMN_SEND)); //get the time data

    java.text.DateFormat df = new java.text.SimpleDateFormat("hh:mm:ss");
    java.util.Date t1 = df.parse(temp);
    java.util.Date t2 = df.parse(temp2);
    long sumSleep = t2.getTime() - t1.getTime();

    //sumSleep should in Hours(Integer)

    if(sumSleep < 8){
        sleep.setImageResource(R.drawable.ic_close);
    }else{
        sleep.setImageResource(R.drawable.ic_check);
    }

}

Aucun commentaire:

Enregistrer un commentaire