简体   繁体   中英

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 DateTimeFormat.forPattern("HH:mm") 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++) {
        timeInt.put(i,time.toString());
        time = time.plusMinutes(minutes);
    }

    return timeInt;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM