java - Integer Time Intervals -
this question more of math problem (sorry, i'm not in math).
i have time represented integer
for example
700 (means 7:00) 1330 (means 13:30) 2359 (means 23:59)
i have starting time , ending time , want create 15 minutes intervals between them. example
int start = 700; int end = 1300;
i want loop on starting time increment 15 minutes , jump on 60 next hundred. example 700, 715, 730, 745, 800, 815, 830.. etc.
i can create creating calendar
object , parse integer simpledateformat
, add time calendar, increase minutes, , parse hours , minutes integer. way long it, there should simple mathematical way perform more efficiently.
if must use integers, or decided to, can create method "plusminutes", like:
public int plusminutes (int time, int minutes) { int auxminutes = time % 60; auxminutes += minutes; time += 100 * (seconds / 60); time = 100 * (time / 100); // set last 2 digits 0 time += auxminutes % 60; return time; }
another option (better in opinion) create method "tominutes" , method "toencoded", like:
public int tominutes (int encoded) { int minutes = encoded % 100; minutes += (encoded / 100) * 60; return minutes; } public int toencoded (int minutes) { int encoded = minutes % 60; encoded += (minutes / 60) * 100; return encoded; }
note: not 100% sure if code works, haven't tested, should general idea.
edit: , problem when using last option, have like:
int encoded = 1330; int plusminutes = 45; int minutes = tominutes(encoded); minutes += plusminutes; encoded = toencoded(minutes);
Comments
Post a Comment