0

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

2

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

First, this does not break your infinite loop from the question. If someone know why intelIJ crosses out "getSeconds()", That’s because you are calling the 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.