Loop in hour interval

0

I need to walk through a time slot to set up a schedule grid. Example: 8:00 AM to 10:00 AM, adding 30 minutes. Forming a grid like this: 8 o'clock 08:30 09:00 09:30 10:00

I'm trying like this:

    GregorianCalendar gc = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

    Date hInicial = null, hFinal = null;

    try {
        hInicial = sdf.parse("08:00");
        hFinal = sdf.parse("10:00");
    } catch (ParseException e) {
        e.printStackTrace();
    }

    GregorianCalendar inicio = new GregorianCalendar();
    inicio.setTime(hInicial);
    GregorianCalendar fim = new GregorianCalendar();
    fim.setTime(hFinal);

    gc.setTime(hInicial);
    while(!inicio.after(fim)) {

        gc.add(Calendar.MINUTE, 30);

    }

The system is in infinite loop.

Thank you in advance.

    
asked by anonymous 06.09.2016 / 15:55

2 answers

0

The problem is that you are testing whether the inicio variable is after fim and within while you do not modify any of the two variables. Try to use inicio instead of gc . Staying like this:

while(!inicio.after(fim)) {

    inicio.add(Calendar.MINUTE, 30);

}
    
06.09.2016 / 16:02
0

You never change the state of inicio or fim in the body of your while , then the value of inicio.after(fim) will never change, causing the infinite loop.

    
06.09.2016 / 16:02