简体   繁体   中英

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

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.

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