Beginners Programs :

Java: Number of Days In A Month – 3 Ways | Java Programs

Java Program To Calculate Number of Days In A Month – In this article, we will detail in on how to calculate the number of days in a month in Java Programming. Suitable examples and sample programs have been included in order to make you understand simply. The compiler has also been added so that you can execute the programs yourself.

The methods used in this article are as follows:

  • Using Scanner Class
  • Using a Static Method
  • Using Separate Class

It is clear to everyone that a single Roman calendar has 12 months in a year. Each month has a distinct number of days in it.

java number of days in a month

As you can see in the image uploaded above, a year has 12 months. Out of it, only February has less than 30 days. Rest of the months have at least 30 days.

February has 28 days in a normal year and 29 days in a leap year.

A leap year is a year if it is divisible by 4, or divisible by 400 if the last two digits are 00.

The months have the following number of days:

  1. January: 31 days
  2. February: 28 / 29 days
  3. March: 31 days
  4. April: 30 days
  5. May: 31 days
  6. June: 30 days
  7. July: 31 days
  8. August: 31 days
  9. September: 30 days
  10. October: 31 days
  11. November: 30 days
  12. December: 31 days

Thus, the methods to calculate the number of days in a month in Java programming are as follows:

Java Code – Number of Days in Month (Using Scanner Class)

Using Scanner class, we take in the inputs at run time as per user’s convenience.

So in here, we initially take the input of month in numeric format followed by which we take the year for which you need the output.

Year is a necessary input because, based on whether it is a leap year of not the days in the month of February change.

After taking the month and year as input, first we check whether it is between 1 to 12 as, there are only 12 months a year.

For the months January (1), March (3), May (5), July(7), August (8), October (10) and December (12) the number of days are 31.

Else, for the months April (4), June (6), September (9) and November (11) the number of days are 30.

For the month February, we check whether the year is a leap year or not. If it’s a leap year the 29 days if not then 28 days. This can be verified based on the conditions given above.

Program :

Output:

Using Static Method

Static method is made use of to make as, static methods do not require object creation and thereby it is memory manageable.

In here, the programming logic is the same as used above.

The difference here is that, this main programming logic is written inside a static method named ‘days’ which is invoked within the main method which consists of various conditions based on which the number of days are decided.

Output:

 

Find Days in Month Java Code – Using Separate Class

 

Apart from the methods discussed above, a little similar to static method, instead of writing in a static method of the same class, we can write the programming logic in a completely different class.

This class will consist of a method preferably constructor in which the logic with all the conditions are written.

A constructor is preferred because as soon as an object is created for this separate class, the constructor will be called implicitly.

So an object of this separate class named “Days” is created in the main method.

As this refers to the days class immediately the constructor is called to give us the number of days.