androidchronometer

Showing the countup timer


I want to show the count up timer in my android app in yy-MM-dd h:m:s format

What I tried: I tried manipulating the CountDownTimer so that it works in reverse order. Changing the interval to -1000 or adding 2000 milliseconds to the Countdown every second. Both didn't work. Then I figured I should use the Chronometer class. The standard Chronometer only displays hours, minutes and seconds as far as I'm concerned. So no days, no months and no years. One more thing that

My code that i wrote

  chronometer.setFormat("%s");
    chronometer.setBase(SystemClock.elapsedRealtime());
    chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer chronometer) {

            String cTextSize = chronometer.getText().toString();

           if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
               chronometer.setText(cTextSize.substring(3,5)+" sec");

            }
            else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 60000)  &&  ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 3600000)){
                chronometer.setText(cTextSize.substring(0,2)+" mins "+cTextSize.substring(3,5)+" sec");
            }
            else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 3600000)  &&  ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 86400000)){
                chronometer.setText(cTextSize.substring(0,2)+" hours "+cTextSize.substring(3,5)+" mins "+cTextSize.substring(6,8)+" sec");
            }
}

Here code runs fine until showing hours but now i want if hours are greater then 24 it shows 1 day not 25th hour

Your help will be much appreciated and if u don't know the answer then kindly don't vote it negative


Solution

  • Well thanks to all google certified developer for helping me. Anyway this is how I solved my problem

    chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
    
                String cTextSize = chronometer.getText().toString();
    
    
                if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
                    chronometer.setText(cTextSize.substring(3, 5) + " sec");
    
                } 
                else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 60000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 3600000)) {
                    chronometer.setText(cTextSize.substring(0, 2) + " mins " + cTextSize.substring(3, 5) + " sec");
                } 
                else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 3600000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 86400000)) {
                    if (chronometer.getText().length() == 8){
                        chronometer.setText(cTextSize.substring(0, 2) + " hours " + cTextSize.substring(3, 5) + " mins " + cTextSize.substring(6, 8) + " sec");
                }
            }
                else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 86400000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 2592000000L))
                {
                    Toast.makeText(getActivity(), "day", Toast.LENGTH_SHORT).show();
    
                    long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
                    final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
                            - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
                    final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
                    final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
                    if(dy==1) {
                        chronometer.setText(dy + " day " + hr + " hours " + min + " mins " + sec + " sec ");
                    }else if(dy>1) {
                        chronometer.setText(dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
                    }
                    }
                else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 2592000000L) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 31556952000L))
                {
                    Toast.makeText(getActivity(), "month", Toast.LENGTH_SHORT).show();
    
                    long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
                    final long mn = dy / 30;
                    dy %= 30;
                    final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
                            - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
                    final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
                    final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
                        chronometer.setText(mn+" months "+dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
                }
    
                else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 31556952000L))
                {
                    Toast.makeText(getActivity(), "year", Toast.LENGTH_SHORT).show();
                    long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
                    final long yr = dy / 365;
                    dy %= 365;
    
                    final long mn = dy / 30;
                    dy %= 30;
                    final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
                            - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
                    final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
                    final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
                        chronometer.setText(yr+" years "+mn+" months "+dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
                }
    
            }
        });