0

In my application there are two buttons. One will show start time and the second one will show the stop time when it's clicked. I need to get those two values, calculate the difference and multiply it by another value. Here is how i set value to one of the labels:

if (v == btnstart) {
    Toast.makeText(getApplicationContext(), "Work Start", Toast.LENGTH_LONG).show();
    Date d=new Date();

    SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
    String currentDateTimeString = sdf.format(d);
    txtstart.setText(currentDateTimeString);
}
zkristic
  • 629
  • 1
  • 9
  • 24
  • Have you tried searching for the same question? http://stackoverflow.com/a/5127822/1178798 `long start = System.currentTimeMillis(); long end = System.currentTimeMillis(); long diff = end - start; DateTime dt = new DateTime(diff);` – zkristic May 14 '15 at 09:35

3 Answers3

2

when you click on StartTime button get the time using System.currentTimeMillis(); in a long variable since the time will be in milliseconds and similarly on StopTimer button u can do the same. After that calculate the difference between the two time which you will get in milliseconds. Just in case if you want to convert the milliseconds to seconds then divide the value by 1000. Hope this helps you.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
frost
  • 520
  • 1
  • 3
  • 13
0

Simply you can use System.currentTimeMillis() for your calculation

if (v == btnstart) {
    Toast.makeText(getApplicationContext(), "Work Start", Toast.LENGTH_LONG).show();
//    Date d=new Date();

//    SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
//    String currentDateTimeString = sdf.format(d);
    startTime = System.currentTimeMillis();
    txtstart.setText(currentDateTimeString);
} else if (v == btnDone) {
    Toast.makeText(getApplicationContext(), "Work Done", Toast.LENGTH_LONG).show();
doneTime = System.currentTimeMillis();
takenTime = doneTime - startTime;
txtDone.setText(takenTime);
}
Kushal
  • 8,100
  • 9
  • 63
  • 82
0

Try

long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
long diff = end - start;
DateTime dt = new DateTime(diff);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53