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.
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);
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With