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.
getSeconds method of Date, a method that has been deprecated for more than 25 years because it works unreliably across time zones, which in turn means that you should not use it. BTW you should neither use the Date nor the Calendar class at all. Use java.time.
Calendar. That class is poorly designed and long outdated. Instead use for exampleLocalTimefrom java.time, the modern Java date and time API.