0

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

}
mango23
  • 49
  • 1
  • 1
  • 9

2 Answers2

0

This gives you the difference in milliseconds:

long sumSleep = t2.getTime() - t1.getTime();

So to according this answer to convert it in hours up to Java 5:

TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(sumSleep))

Or, with Java 4 or less

int hours   = (int) ((sumSleep / (1000*60*60)) % 24);
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

Variable sumSleep contains difference between t1 and t2 in milliseconds. So you just need to convert it to hours.

long millisInHour = 1000 * 60 * 60;
long elapsedHours = sumSleep / millisInHour;
mol
  • 2,607
  • 4
  • 21
  • 40