Buke
Buke

Reputation: 17

How to check whether date is valid

I can use only parseInt and if-else-statements and I'm stuck right now. I understood the logic, but I can't control for example february dates and leap years. I wrote some statements about it.

boolean leap = false;
if (((year % 4) == 0) && ((year % 100) == 0)
        && ((year % 400) == 0) && ((year % 4000) != 0))
    leap = true;
else
    leap = false;

But I can't contact with february. Can you help me please?

Upvotes: 0

Views: 131

Answers (2)

erp
erp

Reputation: 3034

I'd simplify @Matt Jones answer but that's just me.

leap = false;
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
    leap = true;
}

same thing basically but a little less typing :)

Upvotes: 0

Matt Jones
Matt Jones

Reputation: 509

Leap year can be evaluated by:

    if ((year % 4 == 0) && (year % 100 != 0))
    {
        leap = true;
    }
    else if (year % 400 == 0)
    {
        leap = true;
    }
    else
    {
        leap = false;
    }

Upvotes: 1

Related Questions