I want to select records that have a created date between the first and the last day of a given month. I calculate the month with begin and enddate the following way:
The Date “month” is just a random date inside the timeframe
Calendar cal = Calendar.getInstance();
cal.setTime(month);
int endday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int actualmonth = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
Date begin = format.parse("01." + actualmonth + "." + year);
Date end = format.parse(endday + "." + actualmonth + "." + year);
When I think about this, I’d actually have to set the daytime as well, to avoid records going missing on the last day of the month.
I feel this is kinda inelegant, anyone up for a better solution?
What I would do is create a Calendar and set the month and year to the month you want, the day to 1, hour, minute and millisecond to zero. Then take the date of that for your begin range. Then I’d add a month, and then subtract a millisecond. Take the date of that for your end range.