Reputation: 101
How to get all dates in the calendar in current/some month? for example for this month, like the picture
So the result is ["07-31-2016", "08-01-2016", "08-02-2016" ... "08-31-2016", "09-01-2016", "09-02-2016", "09-03-2016"]
Any ideas?, thanks in advance.
Upvotes: 2
Views: 7436
Reputation: 6946
Write a common method like this and use it -
fun getAllDateOfCurrentMonth(): List<LocalDate> {
val yearMonth= YearMonth.now()
val firstDayOfTheMonth = yearMonth.atDay(1)
val datesOfThisMonth = mutableListOf<LocalDate>()
for (daysNo in 0 until yearMonth.lengthOfMonth()){
datesOfThisMonth.add(firstDayOfTheMonth.plusDays(daysNo.toLong()))
}
return datesOfThisMonth
}
Upvotes: 1
Reputation: 4948
You can use the nice java.time classes built into Java 8 and later. Both the above solutions work, this is a way to do in Java 8. Can be done with a little more brevity , split it just for understanding.
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
public class Clazz {
public static void main(String[] args) throws Exception {
LocalDate today = LocalDate.now();
LocalDate firstDayOfTheMonth = today.with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDayOfTheMonth = today.with(TemporalAdjusters.lastDayOfMonth());
LocalDate squareCalendarMonthDayStart = firstDayOfTheMonth
.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
LocalDate squareCalendarMonthDayEnd = lastDayOfTheMonth
.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));
List<LocalDate> totalDates = new ArrayList<>();
while (!squareCalendarMonthDayStart.isAfter(squareCalendarMonthDayEnd)) {
totalDates.add(squareCalendarMonthDayStart);
squareCalendarMonthDayStart = squareCalendarMonthDayStart.plusDays(1);
}
totalDates.forEach(System.out::println);
}
}
Upvotes: 4
Reputation: 1466
Well, with Calendar
and its constants you can achieve this quite easy:
Given month
and year
get first day of the month and place calendar on monday:
Calendar start = Calendar.getInstance();
start.set(MONTH, month - 1); // month is 0 based on calendar
start.set(YEAR, year);
start.set(DAY_OF_MONTH, 1);
start.getTime(); // to avoid problems getTime make set changes apply
start.set(DAY_OF_WEEK, SUNDAY);
if (start.get(MONTH) <= (month - 1)) // check if sunday is in same month!
start.add(DATE, -7);
Given month
and year
get last day of month and move calendar to sunday
Calendar end = Calendar.getInstance();
end.set(MONTH, month); // next month
end.set(YEAR, year);
end.set(DAY_OF_MONTH, 1);
end.getTime(); // to avoid problems getTime make set changes apply
end.set(DATE, -1);
end.set(DAY_OF_WEEK, SATURDAY);
if (end.get(MONTH) != month)
end.add(DATE, + 7);
Test it:
public static void main(String[] args) {
int month = 8, year = 2016;
Calendar start = Calendar.getInstance();
start.set(MONTH, month - 1); // month is 0 based on calendar
start.set(YEAR, year);
start.set(DAY_OF_MONTH, 1);
start.getTime();
start.set(DAY_OF_WEEK, SUNDAY);
if (start.get(MONTH) <= (month - 1))
start.add(DATE, -7);
System.out.println(printCalendar(start));
Calendar end = Calendar.getInstance();
end.set(MONTH, month); // next month
end.set(YEAR, year);
end.set(DAY_OF_MONTH, 1);
end.getTime();
end.set(DATE, -1);
end.set(DAY_OF_WEEK, SATURDAY);
start.getTime();
if (end.get(MONTH) != month)
end.add(DATE, + 7);
System.out.println(printCalendar(end));
}
Combined with:
import static java.util.Calendar.*;
and
private final static SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
private static String printCalendar(Calendar c) {
return df.format(c.getTime());
}
OUTPUT:
2016/07/31
2016/09/03
WITH
int month = 5, year = 2015;
OUTPUT:
2015/04/26
2015/06/06
Now, just iterate over starting Calendar
adding +1
to Calendar.DATE
in a while
loop (in the example I split by weeks to be more clear):
int i = 1;
while (start.before(end)) {
System.out.print(printCalendar(start));
if (i % 7 == 0) { // last day of the week
System.out.println();
i = 1;
} else {
System.out.print(" - ");
i++;
}
start.add(DATE, 1);
}
OUTPUT:
2015/04/26 - 2015/04/27 - 2015/04/28 - 2015/04/29 - 2015/04/30 - 2015/05/01 - 2015/05/02
2015/05/03 - 2015/05/04 - 2015/05/05 - 2015/05/06 - 2015/05/07 - 2015/05/08 - 2015/05/09
2015/05/10 - 2015/05/11 - 2015/05/12 - 2015/05/13 - 2015/05/14 - 2015/05/15 - 2015/05/16
2015/05/17 - 2015/05/18 - 2015/05/19 - 2015/05/20 - 2015/05/21 - 2015/05/22 - 2015/05/23
2015/05/24 - 2015/05/25 - 2015/05/26 - 2015/05/27 - 2015/05/28 - 2015/05/29 - 2015/05/30
2015/05/31 - 2015/06/01 - 2015/06/02 - 2015/06/03 - 2015/06/04 - 2015/06/05 - 2015/06/06
Upvotes: 4
Reputation: 337
Get the monday before the 1st of that month:
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(2016, 08, 01);
Calendar start = Calendar.getInstance();
start.setFirstDayOfWeek(Calendar.MONDAY);
start.setWeekDate(2016,c.getWeekYear(), Calendar.MONDAY);
Get the sunday after the last day of that month:
c.set(2016,08,31);
Calendar end = Calendar.getInstance();
end.setFirstDayOfWeek(Calendar.MONDAY);
end.setWeekDate(2016, c.getWeekYear(), Calendar.SUNDAY);
Then print all dates between start and end
Upvotes: 1