Link Details

Link 583029 thumbnail
User 326913 avatar

By haivu
via wuhrr.wordpress.com
Published: Apr 10 2011 / 08:03

Given a year and a month, I want to determine the last day of that month. For example, if the year is 2004 and the month is 2, then the last day is 29th because of leap year.
  • 7
  • 2
  • 2793
  • 5

Comments

Add your comment
User 191160 avatar

smc replied ago:

2 votes Vote down Vote up Reply

A slightly improved algorithm
Step 1: Check your language's libraries to see how they are used
e.g. in java

Calendar calendar = Calendar.getInstance();
calendar.set(2004, Calendar.FEBRUARY, 1);
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("Max Day: " + maxDay);


in python
calendar.monthrange(2004, 2)[1]

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (7)



Voters Against This Link (2)



Apache Hadoop
Written by: Piotr Krewski
Featured Refcardz: Top Refcardz:
  1. Play
  2. Akka
  3. Design Patterns
  4. OO JS
  5. Cont. Delivery
  1. Play
  2. Java Performance
  3. Akka
  4. REST
  5. Java