user3786045 user3786045 - 8 months ago 28
Java Question

Incrementing time by a specified interval using joda library

public Map<Integer , String> Timeinterval(int value)
int i = 1440/value ;
Map<Integer, String> timeInt = new HashMap<Integer, String>() ;
DateTimeFormatter formatter = null ;
for(int a=0 ; a< i ; a++)
formatter = DateTimeFormat.forPattern("HH:mm");
LocalTime time = formatter.parseLocalTime("09:00");
time = time.plusMinutes(value);

timeInt.put(a,time.toString() );

return timeInt;

The argument value gets it value from JSP at runtime

The moment the control reaches at
formatter = DateTimeFormat.forPattern("HH:mm")
it could not go beyond this.
I executed this part
by pressing cntrl+shift+I and got this message:

could not resolve type: org.joda.time.format.DateTimeFormat

Let me know if anything else is wrong with the code

Basically what I want to do is increment time for eg: if time is 09:00 and I want to increment it by 30 then it should become 09:30 and this goes on till the time loop condition is satisfied. When I researched on net, I found out that Joda library is easier to work with for such tasks.


Your question is not clean. The LocalTime of Joda time does support the plusMinutes(int) which is exactly what you are looking for.

Of course you need to set the time outside the loop:

Map<Integer, String> timeInterval(int perDay)
    int minutes = 1440/perDay ;
    Map<Integer, String> timeInt = new HashMap<Integer, String>() ;
    LocalTime time =  DateTimeFormat.forPattern("HH:mm").parseLocalTime("09:00");

    for(int i=0; i < perDay; i++) {
        time = time.plusMinutes(minutes);

    return timeInt;