Вопрос

I am using Joda DateTime and have 2 dates :

DateTime old //which is 1:46PM   
DateTime new //which is 6:46PM

note: excluded the dates.

How may i be able to loop through the difference in this order :

(print a message for the first half hour, another message for the next 30 minutes and another message per subsequent hour) ?

I was thinking of Subtracting the old date from the new date then make a loop but i don't get the logic. Any pointers will be helpful.

example
If i subtract both times above, i will have an elapsed time of 5 hours.

Loop 5 hours
{
   for the first hour (print this)
   next 30minutes (print that)
   every subsequent hour (print ....)
}
Это было полезно?

Решение

I would use the type LocalTime instead. If you have DateTime as input then please convert it using the method toLocalTime() (with same time and chronology and timezone).

LocalTime start = new LocalTime(13, 46);
LocalTime end = new LocalTime(18, 46);
LocalTime current = start;

for (int i = 0; current.isBefore(end); i++) {
    // code your print action here
    current = current.plusMinutes((i < 2) ? 30 : 60);
}

Then you get an action for following times:

13:46
14:16
14:46
15:46
16:46
17:46
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top