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

  • 25 May

    Format Date to yyyy-MM-dd in java

    Table of ContentsUsing DateTimeFormatter with LocalDate (Java 8)Using SimpleDateFormatUsing Calendar classWas this post helpful? In this post, we will see how to Format Date to yyyy-MM-dd in java. There are multiple ways to format Date to yyyy-MM-dd in java. Let’s go through them. Using DateTimeFormatter with LocalDate (Java 8) To Format Date to yyyy-MM-dd in […]

  • Get last day of month in java
    04 February

    Get last day of Month in java

    Table of ContentsWays to get last day of month in javaUsing LocalDate to get last day of month in Java 8Using Calendar to get last day of month in javaGet last day of previous month in javaGet last day of next month in javaWas this post helpful? Learn about how to get last day of […]

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.DateWas this post helpful? In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    Table of ContentsUsing Instant objectUsing java.sql.DateWas this post helpful? In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using […]

  • 30 December

    How to get day name from date in java

    Learn about how to get day name of week from date in java

  • 22 December

    How to add days to date in java

    Learn about how to add days to Date using Calendar and LocalDate in java.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.