Solved

No of Days in month

Posted on 2000-05-11
5
216 Views
Last Modified: 2010-04-01
I wanna know if there is any methods in API which can give the No of days in a given month of the year.
I know writing the code is easier,but just curious.
0
Comment
Question by:nellore
5 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 2802498
Try using the Calendar class. The easiest demonstration is number of days for the current month:

Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

You can do the same for any arbitrary date by initializing the Calendar object with a specific date before asking for the maximum:

// set to 1-feb-2000 (month is 0-based)
cal.setTime(2000, 1, 1);

Since Calendar knows leap years, asking for maximum DAY_OF_MONTH will return 29 as 2000 is a leap year.

Best regards,
Jim Cakalic

0
 
LVL 1

Author Comment

by:nellore
ID: 2802515
Thanks for the answer.

Actually
cal.setTime(2000, 1, 0)
is returning the Date set to the last day of Jan(not 0-based).


0
 

Expert Comment

by:sloJava
ID: 2802517
There is not explicit method in the API for doing this.
I came across this clever nugget some where...

int days = 32 - new Date(year, month, 32).getDate();
0
 

Expert Comment

by:maheshsivakumar
ID: 2803880
Yeah the best method should be what jim has stated.
 It gives the maximum no of days of any month if the Calendar object is initialized correctly.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2804366
By using set(2000, 1, 0) you have initialized Calendar to the 0th day of month 1 -- the 0th day of February. There is no 0th day of February (or any month) so Calendar normalizes the value on your behalf and ends up with the 31st of January. This is the lenient mode of Calendar. However, do not be misled. Calendar definitely treats months as 0-based, everything else is 1-based. If you have programmed in C/C++, you may recognize this as behavior similar to that surrounding interpretation of struct tm. I never understood why a 0-based month is necessary but I have come to accept it. One way to verify this is to look at the values of the month constants in Calendar (JANUARY, FEBRUARY, ...). If you don't have the code, you can simply print the values:

    system.out.println("Jan = " + Calendar.JANUARY);
    System.out.println("Dec = " + Calendar.DECEMBER);

This fragment of code will print:

    Jan = 0
    Dec = 11

You can disable the ability to initalize Calendar with "invalid" dates by changing the lenient mode. The following bit of code will result in an InvalidArgumentException.

    Calendar cal = Calendar.getInstance();
    cal.setLenient(false);
    cal.set(2000, 1, 0);

Best regards,
Jim Cakalic
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array6 challenfge 6 62
countHi2 challenge 7 44
Free Alternative to JIRA 4 55
@SBGen Method 3 25
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now