将时间划分为相等的间隔

时间:2017-12-23 11:35:18

标签: java android

我想,如果用户输入tofrom时间,则代码应计算30分钟的时间间隔,但except字段中提及的时间除外。

这样的东西

如果输入

 from=10:00
 to=14:00
 except
    from=11:10
    to=11:45
    and 
    from=13:10
    to=13:30

然后输出

10:00
10:30
12:00
12:30
13:30

我的代码类似于:

DateFormat df = new SimpleDateFormat("HH:mm");
    Calendar startTime = Calendar.getInstance();
    startTime.set(Calendar.HOUR_OF_DAY, 8);
    startTime.set(Calendar.MINUTE, 0);


    Calendar endTime = Calendar.getInstance();
    endTime.set(Calendar.HOUR_OF_DAY, 17);
    endTime.set(Calendar.MINUTE, 30);

   while (startTime.get(Calendar.HOUR_OF_DAY)!=endTime.get(Calendar.HOUR_OF_DAY)&&startTime.get(Calendar.MINUTE)!=endTime.get(Calendar.MINUTE)) {
        System.out.println(df.format(startTime.getTime()));
        startTime.add(Calendar.MINUTE, 30);
    }

2 个答案:

答案 0 :(得分:0)

while应该有!=而不是==。除外应该是另一个条件。循环运行时,条件内部为真

答案 1 :(得分:0)

经过一小时的RND,我找到了解决方案。

ExceptDate的模型类

public class ExceptDate{

ExceptDate(String from,String to){
    this.from=from;
    this.to=to;
}

private String from;

private String to;

public void setFrom(String from){
    this.from = from;
}

public String getFrom(){
    return from;
}

public void setTo(String to){
    this.to = to;
}

public String getTo(){
    return to;
}

}

输入变量:

String strFromTime = "10:00";
    String strToTime = "14:00";
    ArrayList<ExceptDate> exceptTime = ArrayList < String > ()
    exceptTime.add(new ExceptDate("11:10", "11:45"));
    exceptTime.add(new ExceptDate("13:10", "13:30"));

魔法发生的最终方法。

public ArrayList<String> getTimeSlot(int slotMinute, strFromTime, strToTime, exceptTime) {
    int fromHour, fromMinute, toHour, toMinute;

    fromHour = Integer.parseInt(strFromTime.split(":")[0]);
    fromMinute = Integer.parseInt(strFromTime.split(":")[1]);

    toHour = Integer.parseInt(strToTime.split(":")[0]);
    toMinute = Integer.parseInt(strToTime.split(":")[1]);

    long slot = slotMinute * 60 * 1000;
    Calendar calendar2 = Calendar.getInstance();
    calendar2.set(Calendar.HOUR_OF_DAY, fromHour);
    calendar2.set(Calendar.MINUTE, fromMinute);

    long currentTime = calendar2.getTimeInMillis();
    Calendar calendar1 = Calendar.getInstance();
    calendar1.set(Calendar.HOUR_OF_DAY, toHour);
    calendar1.set(Calendar.MINUTE, toMinute);

    long endTime = calendar1.getTimeInMillis();

    ArrayList<String> timeSlot = new ArrayList<String>();

    while (currentTime < endTime) {
        if (availableDatesItem.getExcept() == null) {
            timeSlot.add(sdfTime.format(new Date(currentTime)));
            currentTime = currentTime + slot;
        } else {
            for (int i = 0; i < exceptTime.size(); i++) {

                int exceptFromHour, exceptFromMinute, exceptToHour, exceptToMinute;

                exceptFromHour = Integer.parseInt(exceptTime.get(i).getFrom().split(":")[0]);
                exceptFromMinute = Integer.parseInt(exceptTime.get(i).getFrom().split(":")[1]);

                exceptToHour = Integer.parseInt(exceptTime.get(i).getTo().split(":")[0]);
                exceptToMinute = Integer.parseInt(exceptTime.get(i).getTo().split(":")[1]);

                Calendar cal2 = Calendar.getInstance();
                cal2.set(Calendar.HOUR_OF_DAY, exceptFromHour);
                cal2.set(Calendar.MINUTE, exceptFromMinute);

                long exceptFrom = cal2.getTimeInMillis();
                Calendar cal1 = Calendar.getInstance();
                cal1.set(Calendar.HOUR_OF_DAY, exceptToHour);
                cal1.set(Calendar.MINUTE, exceptToMinute);

                long exceptTo = cal1.getTimeInMillis();
                if (currentTime < exceptFrom) {
                    timeSlot.add(sdfTime.format(new Date(currentTime)));
                    currentTime = currentTime + slot;
                } else if (currentTime > exceptFrom && currentTime > exceptTo) {
                    timeSlot.add(sdfTime.format(new Date(currentTime)));
                    currentTime = currentTime + slot;
                } else {
                    currentTime = currentTime + slot;
                }
            }
        }
    }
    return timeSlot;
}