0

I have two variables - time when activity starts (currentTime) and time when the button is clicked (endTime). These variables have values in milliseconds (System.currentTimeMillis()). I use them in Layout so I convert these values to DateFormat "HH:mm:ss".

I want to calculate duration (durationTime) between currentTime and endTime and convert it to datetime. Look please to my code:

public class MainActivity extends Activity {

            long currentTime_ms, endTime_ms;
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                currentTime_ms = System.currentTimeMillis();
                Date currentTime = new Date(currentTime_ms);
                currentTime_textView.setText(sdf.format(currentTime));
            }

            public void btnOk_onClick(View v) {

                endTime_ms = System.currentTimeMillis();
                Date endTime = new Date(endTime_ms);
                Date durationTime = new Date(endTime_ms - currentTime_ms);
            }

        }

Finally I've got wrong duration. (For example I click on Button after 2 second and get durationTime 02:00:02).

Can you help me to fix the code? Or maybe there are other methods for time calculation?

Aleksey Kurkov
  • 323
  • 2
  • 13
  • First of all you have bad onClickListener. When You have activity, you have to set the listener on button: `Button btn = (Button)findViewById(R.id.button_id); btn.setOnClickListener(new OnClickListener{ @Override public void onClick(){ //code here } }` – Mariusz Brona Oct 18 '14 at 20:36
  • Ok, thanks for advice. I will use OnClickListener. And what about time calculations? – Aleksey Kurkov Oct 18 '14 at 20:43
  • It should be really simple. You have the global variables so its good. Next just look into it for example: http://stackoverflow.com/questions/21285161/android-difference-between-two-dates – Mariusz Brona Oct 18 '14 at 20:45

1 Answers1

0

You can get current time with SystemClock.elapsedRealtime();

  • Take two timestamps (for beginning and ending) and then you'll have two different longs.
  • Calculate the difference between them, and call this function, which will return the String that you are looking for. You can customize it as you want!!

    public String toText(long difference) {
        DecimalFormat df = new DecimalFormat("00");
    
        int remaining = (int) (difference % (3600 * 1000));
    
        int minutes = (int) (remaining / (60 * 1000));
        remaining = (int) (remaining % (60 * 1000));
    
        int seconds = (int) (remaining / 1000);
        remaining = (int) (remaining % (1000));
    
        int cents = (int) (((int) time % 1000) / 10);
    
        String text = "";
        text += df.format(minutes) + ":";
        text += df.format(seconds) + ":";
        text += df.format(cents);
        return text;
    }
    

In your case, this should return "00:02:00": 0 minutes, 2 seconds, 0 cents.

I hope this would helps!!

Juan Aguilar Guisado
  • 1,687
  • 1
  • 12
  • 21