Search code examples
javaandroiddatebuttonclickmilliseconds

Duration for two button clicks returns false values


I'm trying to calculate break time user takes by click on before user takes break and click on another button when he finish break. I stumbled upon few questions and read that best method is to use nanoTime() method.

System.nanoTime();

But the problem is when I calculate the duration it returns weird value.

Screenshot

Here is my code,

        btnStartBreak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                breakStartTime = System.nanoTime();
                breakTv.setText("Do you want to finish the break?");
                btnStartBreak.setVisibility(View.GONE);
                btnFinishBreak.setVisibility(View.VISIBLE);
            }
        });

        btnFinishBreak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                breakFinishTime = System.nanoTime();
                long breakTime = breakFinishTime - breakStartTime;
                Toast.makeText(getApplicationContext(), getDate(TimeUnit.NANOSECONDS.toMillis(breakTime), "mm:ss:SSS"), Toast.LENGTH_LONG).show();
            }
        });

        public static String getDate(long milliSeconds, String dateFormat){
        // Create a DateFormatter object for displaying date in specified format.
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);

        // Create a calendar object that will convert the date and time value in milliseconds to date.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliSeconds);
        return formatter.format(calendar.getTime());
        }

I have used this link to convert milliseconds to hours and minutes.


Solution

  • I'll provide my comment as answer here with additional info.

    You're using System.nanotime() which, according to the docs,

    Returns the current value of the most precise available system timer, in nanoseconds.

    But your method getDate() requires the time in milliseconds. So the value you provide is 1,000,000 times too big.

    Instead, you can use System.currentTimeMillis() which will give you the needed time in milliseconds.

    Edit:

    It seems that you want to calculate a timespan and display it in the form minutes:seconds:millis. Don't use Calendar and/or Date for that. And you don't need SimpleDateFormat.

    You already have the time difference between the two actions. There's a much simpler approach to format that timespan. You can use the following snippet for converting:

    public static String formatMillis(long ms) {
        long rest = ms;
        long minutes = rest / (1000 * 60);
    
        rest = rest % (1000 * 60);
        long seconds = rest / 1000;
        long millis = rest % 1000;
    
        return String.format("%02d:%02d:%03d", minutes, seconds, millis);
    }
    

    Input:

    formatMillis(5000L);
    formatMillis(60000L);
    formatMillis(67890L);
    

    Output:

    00:05:000
    01:00:000
    01:07:890