• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find the total number of days in the month using LocalDateTime in Java 8?  RSS feed

 
Hasnain Ahmad
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using java 8 date and time classes in my code. I can find year, month, day, hours, minutes and seconds, but I cannot find a method to find a number of days in a month.

Here is my code.

I also want to iterate through “oldestDate” to “latestDate”. How can I iterate through “oldestDate” to “latestDate” until the “oldestDate” reaches to “latestDate”?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

The new date and time API has everything you need to do with dates and times. For example:


To iterate through a range of dates:


If you have a LocalDateTime object instead of a LocalDate object, you can call .toLocalDate() on it to make it a LocalDate.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a method to get the month object? Surely the Month enum will have methods for number of days.

And welcome to the Ranch again.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And LocalDate has a lengthOfMonth() method to get the number of days in the month represented by the LocalDate object
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!