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?