Get number of days in Month in java

Learn about how to get number of days in Month in java.

Get number of days in Month in java

Using YearMonth’s lengthOfMonth() [Java 8]

You can use YearMonth‘s lengthOfMonth() method to get number of days in Month in java.

  • Pass Year and month to YearMonth’s of and create YearMonth object.
  • Call lengthOfMonth() on YearMonth’s object to get number of days in Month in java.

Output:

Number of days in Feb 2019: 28
Number of days in Feb 2020: 29

Using LocalDate’s lengthOfMonth [Java 8]

You can use LocalDate‘s lengthOfMonth() method to get number of days in Month in java.

  • Pass Year and month to LocalDate’s of and create YearMonth object.
  • Call lengthOfMonth() on LocalDate’s object to get number of days in Month in java.

Output:

Number of days in Feb 2019: 28
Number of days in Feb 2020: 29

Using Calendar

You can use Calendar’s getActualMaximum() to get number of days in Month in java.

  • Create instance of Calendar object.
  • Call Calendar’s getActualMaximum() to get number of days in Month in java.

Output:

Number of days in Feb 2019: 28
Number of days in Feb 2020: 29

That’s all about how to get number of days in Month in java

Was this post helpful?

Related Posts

  • 26 November

    Check if Date Is Between Two Dates in Java

    Table of ContentsIntroductionDate & Local Date Class in JavaCheck if The Date Is Between Two Dates in JavaUsing the isAfter() and isBefore() Methods of the Local Date Class in JavaUsing the compareTo() Method of the Local Date Class in JavaUsing the after() and before() Methods of the Date Class in JavaUsing the compare To() Method […]

  • 05 October

    Find Difference Between Two Instant in Java

    Table of ContentsWays to find difference between two Instant in JavaUsing Instant’s until() methodUsing ChronoUnit EnumUsing Duration’s between() method In this post, we will see how to find difference between two Instant in Java. Ways to find difference between two Instant in Java There are multiple ways to find difference between two Instant in various […]

  • 05 October

    Find Difference Between Two LocalDate in Java

    Table of ContentsWays to find difference between two LocalDate in JavaUsing LocalDate’s until() methodUsing ChronoUnit EnumUsing Duration’s between() methodUsing Period’s between() methodFrequently asked questionsHow to find difference between two LocalDate in Java in Days?How to find difference between two LocalDate in Java in Years?How to find difference between two LocalDate in Java in Months?How to […]

  • 26 September

    Get List of Weekday Names in Java

    Table of ContentsUsing DateFormatSymbols’s getWeekdays() methodUsing DateFormatSymbols’s getShortWeekdays() method [For short names]Using DateFormatSymbols’s getWeekdays() with Locale In this post, we will see how to get list of weekday names in Java. Using DateFormatSymbols’s getWeekdays() method We can use DateFormatSymbols's getWeekdays() to get list of weekday names in Java. It will provide complete weekday names like […]

  • 24 September

    Get List of Month Names in Java

    Table of ContentsUsing DateFormatSymbols’s getMonth() methodUsing DateFormatSymbols’s getShortMonths() method [ For short names]Using DateFormatSymbols’s getMonth() with Locale In this post, we will see how to get list of months name in Java. Using DateFormatSymbols’s getMonth() method We can use DateFormatSymbols's getMonth() to get list of months in Java. It will provide complete month names like […]

  • 20 September

    Get Unix Timestamp in Java

    Table of ContentsWays to get unix timestamp in JavaUsing Instant classUsing System.currentTimeMillis()Using Date’s getTime() methodConvert Date to unix timestamp in JavaUsing Instant classUsing Date’s getTime() method In this post, we will get unix timestamp in Java. As per wikipedia Unix time is a system for describing a point in time. It is the number of […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.