I need to format seconds and minutes from milliseconds. I am using countdownTimer. does anyone have sugestions? I looked at joda time. But all i need is a format so i have 1
I used Apache Commons StopWatch class. The default output of it's toString method is ISO8601-like, hours:minutes:seconds.milliseconds.
Example of Apache StopWatch
A real lazy way of doing this as long as you know you won't have more than 60 minutes is to just make a date and use SimpleDateFormat
public void onTick(long millisUntilFinished) {
SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date(millisUntilFinished);
text.setText("Time left:" + dateFormat.format(date));
}
i'd use
org.apache.commons.lang.time.DurationFormatUtils.formatDuration(millisUntilFinished, "mm:ss")
You could do it using the standard Date formatting classes, but that might be a bit heavy-weight. I would just use the String.format method. For example:
int minutes = time / (60 * 1000);
int seconds = (time / 1000) % 60;
String str = String.format("%d:%02d", minutes, seconds);