Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer Time Intervals

Tags:

java

math

time

This question is more of a math problem (Sorry, I'm not good in Math).

I have time represented as integer

for example

700 (means 7:00)

1330 (means 13:30)

2359 (means 23:59)

I have a starting time and ending time and want to create 15 minutes intervals between them. For example

int start = 700;
int end = 1300;

I want to loop over the starting time to increment 15 minutes and jump over 60 to next hundred. For example 700, 715, 730, 745, 800, 815, 830.. etc.

I can create this by creating a Calendar object and parse the integer as SimpleDateFormat, add the time to calendar, increase the minutes, and parse the hours and minutes back to integer. But its way too long to do it, there should be a simple mathematical way to perform this more efficiently.

like image 730
fareed Avatar asked Dec 13 '25 08:12

fareed


2 Answers

Use the LocalTime class. This has a plusMinutes method you can use to increment your base time by 15 minutes.

Maybe something like

LocalTime baseTime = LocalTime.of(((int)myNumber/100),myNumber%100);
while(...){
   LocalTime myTime=baseTime.plusMinutes(15);
}
like image 190
user1717259 Avatar answered Dec 14 '25 20:12

user1717259


To convert an "integer encoded" time to minutes:

int encoded = 2359;
int minutes = (encoded / 100) * 60 + encoded % 100;

Once you have minutes, you can add minutes, or milliseconds:

long milliseconds = minutes * 3_600_000L;
like image 24
Bohemian Avatar answered Dec 14 '25 22:12

Bohemian