Does anyone know where can I find how to calculate how many days February has for a given year?

Recommended Answers

All 5 Replies

A Calendar object set to the proper year and month and the getActualMaximum method.

See the API doc for Calendar.


But, if you want (for whatever reason) to figure it out the "old-fashioned way" then convert the following into a short algorithm.

Feb has 29 days in every year evenly divisible by 4, excpet when the year is also evenly divisble by 100, unless the year is also evenly divisble by 400.

dont know about the standard library function but u can mannualy do it in 2 lines..

int days_in_feb=28;
if(do it urself....cant help)
commented: Don't do peoples (home)work for them. -1

Don't do peoples (home)work for them.

Edit: It doesn't help them, in the least.

what's wrong?? everybody knows this much...and i have just written in code what u wrote in formal english...

do u think i did his homework by writing one if statement...:-O

sorry then
i did that just to help him...i dont have time to do homeworks..:@

commented: And it wasn't even a useful suggestion -1

Actually I already knew about Calendar but I couldn't get the right results. That's why I was looking for the algorithm rajatC posted. The funny thing is that yesterday Calendar wasn't giving me the right results but today for some strange reason the code that I wrote worked. That's why I try to avoid Calendar and work only with Date and SimpleDateFormat. I find it simpler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.