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
If time is less then 1 min then timer only shows seconds like this
00 sec
If less then 1 hour then only minutes and seconds then format should be
00 min 00 sec
If less then 1 day then only hours, mins and seconds
00 hours 00 min 00 sec
If less them 1 month then only show days,hours,mins,secs
0 days 00 hours 00 min 00 sec
If less then one years then only show months,days,hours,mins,secs
0 months 0 days 00 hours 00 min 00 sec
If more then a year then shows like
0 years 0 months 0 days 00 hours 00 min 00 sec
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