简体   繁体   中英

need to find every X minutes between to hours

I have two hours lets say 12:00 and 14:00 (the input will always be two hours of "whatever" day) second time will always be greater than first time. So the input is simple : two given times in a day. I need to find every 30 minutes between those two times.

I need the following output (the ellapsed time between each output will always be 30 minutes in my case:

12:00 12:30 13:00 13:30 14:00 14:30

I am discovering jodatime but I am a bit confused with how the determine "startTime" and "end Time"

It's pretty easy, actually:

DateTime current = start;
while(true){
    System.out.println(current);
    current=current.plusMinutes(30);
    if(current.isAfter(stop))break;
}

Responding to comment:

to parse a String, you need a DateTimeFormatter . Here's one of several ways to acquire one:

private static final DateTimeFormatter DATE_TIME_FORMATTER = 
    DateTimeFormat.forPattern("HH:mm");

Now you can do:

DateTime startDate = DATE_TIME_FORMATTER.parseDateTime(startString);

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