So I have a variable time. It counts the minutes. Is there a way I can format this into minutes easily? For example, time = 63
would equal
1:03
and time = 605
would equal
10:05.
Thank you in advance!
So I have a variable time. It counts the minutes. Is there a way I can format this into minutes easily? For example, time = 63
would equal
1:03
and time = 605
would equal
10:05.
Thank you in advance!
Try,
int totSec= 605;
int min=totSec/60;
int second=totSec%60;
System.out.printf("%d:%02d\n",min,second);
SimpleDateFormat can render any time format you want including the above.
For the Date object you just need to set it to the number of milliseconds since epoch http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#Date(long)
new SimpleDateFormat("HH:mm").format(new Date(minutes * 1000 * 60))
The only problem with the solution above is if time is past 23:59 it will roll over.
Simply create a new
Calendar
and add the amount of minutes. You don't need the date part, just the time. For this reason I reset everything manually in the calendar. By using aCalendar
you also have a lot more flexibility with other date-appropriate calculations.Output: