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?

Leave a Reply

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