Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get the list of days of each month in the "JODA" library?

I need to get a list of days each month in the JODA library. how can I do that?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

tl;dr

Using java.time, the successor to Joda-Time.

yearMonth               // An instance of `java.time.YearMonth`.
.atDay( 1 )             // Returns a `LocalDate` object for the first of the month.
.datesUntil(            // Get a range of dates.
    yearMonth
    .plusMonths( 1 )    // Move to the following month.
    .atDay( 1 )         // Get the first day of that following month, a `LocalDate` object.
)                       // Returns a stream of `LocalDate` objects.
.toList()               // Collects the streamed objects into a list. 

For older versions of Java without a Stream#toList method, use collect( Collectors.toList() ).

java.time

The Joda-Time project is now in maintenance mode. The project recommends moving to its successor, the java.time classes defined in JSR 310 and built into Java 8 and later. Android 26+ has an implementation. For earlier Android, the latest Gradle tooling makes most of the java.time functionality available via « API desugaring ».

YearMonth#lengthOfMonth

Specify a month.

YearMonth ym = YearMonth.now() ;

Interrogate for its length.

int lengthOfMonth = ym.lengthOfMonth() ;

LocalDate

To get a list of dates, get the first date of the month.

LocalDate start = ym.atDay( 1 ) ;

And the first day of the next month.

LocalDate end = ym.plusMonths( 1 ).atDay( 1 ) ;

Get a stream of dates in between. Collect into a list.

List< LocalDate > dates = start.datesUntil( end ).toList() ;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading