Important to say - there are tons of libraries who will give you this specific need, but I would like relying on one that does the work and was actually designed for (some of yours...) those use cases -
Java.time.LocalDate library (already built into Java 8)
import java.time.LocalDate;
LocalDate now = LocalDate.now(); // 2019-04-26
In order to get first and last days of month, you can use:
LocalDate start = YearMonth.now().atDay(1);
(now can be some other month, of course)
LocalDate end = YearMonth.now().atEndOfMonth();
You can use it specifically on one / two months, or with some for loop. Examples below:
1. Specific call:
LocalDate earlierOneMonth = now.minusMonths(1); // 2019-03-26
earlierOneMonth.getDay(); // 26
2. For Loop: (so you'll need something like an array / list to store those values...)
for(int i=0; i < lastMonths - 1; i++){
arr(i) = now.minusMonths(i + 1);
}
Also, in order to get the name of the month, you can use ->
earlierOneMonth.getMonth(); // APRIL
earlierOneMonth.getMonth.getValue(); // 04
Lastly, in order to get the year, you can use ->
earlier.getYear(); // 2019
Once you have all of your desired values, you can print them out as you requested, with that expected output:
"current month is" + nowMonth + "-" + nowYear + " ,1st date is" + nowDay + "-" + nowMonth + "-" + nowYear + " and last date is ...
Let me know if it's clear enough :)