0

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?

msrd0
  • 7,816
  • 9
  • 47
  • 82
Buke
  • 17
  • 5

2 Answers2

1

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;
    }
Matt Jones
  • 511
  • 3
  • 12
0

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 :)

erp
  • 2,950
  • 9
  • 45
  • 90
  • If you want to reduce typing, why are you still repeating the useless `if` statement and all these annoying braces? Just `leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;` is enough. – Holger Oct 14 '14 at 19:42