How do you find the number of days in a month in Java?
If the month is on a knuckle, it has 31 days. Otherwise is has 30 or less days.
yearsBetween() method to calcualte the number of months and years between two dates in Java. LocalDate jamesBirthDay = new LocalDate(1955, 5, 19); LocalDate now = new LocalDate(2015, 7, 30); int monthsBetween = Months.
Number of days in any month of a year can vary specifically in February as the cycle of leap year repeats in every 4 years when the year is leap February gives the count to 29 days but the when the year is not leap it gives count to 28 days and so no of days in a year varies from 365 to 366.
The months having 31 days in a year are January, March, May, July, August, October, and December.
Set the year and month on a Calendar object and then use getActualMaximum
to return the last day:
calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
Since Java 8, a simple way would be:
int daysInCurrentMonth = java.time.LocalDate.now().lengthOfMonth();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With