Search code examples
javaloopscalendarinfinite-loopjava.util.calendar

Why this loop is infinity? [Java]


Why this code is an infinity loop? How I can fix it?

Calendar cal = new GregorianCalendar();
cal.setTime(new Date());

while (cal.SECOND < 20){
    System.out.println(cal.SECOND);
}

Thanks you in advance for yours help


Solution

  • I will get answer, SECOND is a constant variable. If you will get return Second with Calendar class you will wrote:

    cal.get(Calendar.SECOND); 
    //or
    cal.getTime().getSeconds();
    

    If someone know why intelIJ crosses out "getSeconds()", but this working please enter answer here or know what is, a difference between these codes.

    Thanks you in advance for yours help.