/** * Constructor. */ public CalendarModel() { currentMonth = new Date(); CalendarUtil.setToFirstDayOfMonth(currentMonth); // Finding day of week names Date date = new Date(); for (int i = 1; i <= DAYS_IN_WEEK; i++) { date.setDate(i); int dayOfWeek = date.getDay(); dayOfWeekNames[dayOfWeek] = getDayOfWeekFormatter().format(date); } // Finding day of month names date.setMonth(0); for (int i = 1; i < MAX_DAYS_IN_MONTH; ++i) { date.setDate(i); dayOfMonthNames[i] = getDayOfMonthFormatter().format(date); } // finding month names date.setDate(1); for (int i = 0; i < MONTHS_IN_YEAR; ++i) { date.setMonth(i); monthOfYearNames[i] = getMonthFormatter().format(date); } }
private Date firstDayOfMonth(Date month) { Date firstDay = (Date) month.clone(); CalendarUtil.setToFirstDayOfMonth(firstDay); return justDay(firstDay); }
@SuppressWarnings("deprecation") public static Date getSameOrClosestDateInNextMonth(Date date) { Date desiredDate = new Date(date.getTime()); CalendarUtil.addMonthsToDate(desiredDate, 1); if (desiredDate.getMonth() > date.getMonth() + 1) { // skipped one month, e.g. 30 January -> 2nd (or 1st for leap-year) March, because February does not have 30th // set the date to last day of previous month CalendarUtil.setToFirstDayOfMonth(desiredDate); CalendarUtil.addDaysToDate(desiredDate, -1); } return desiredDate; }
@SuppressWarnings("deprecation") public static Date getSameOrClosestDateInPreviousMonth(Date date) { Date desiredDate = new Date(date.getTime()); CalendarUtil.addMonthsToDate(desiredDate, -1); if (desiredDate.getMonth() == date.getMonth()) { // did not go one month back // e.g. 31 May -> 1st May, because April does not have 31st and thus the day is set to 1st May CalendarUtil.setToFirstDayOfMonth(desiredDate); CalendarUtil.addDaysToDate(desiredDate, -1); } return desiredDate; }
private Date lastDayOfMonth(Date month) { Date lastDay = (Date) month.clone(); CalendarUtil.addMonthsToDate(lastDay, 1); CalendarUtil.setToFirstDayOfMonth(lastDay); CalendarUtil.addDaysToDate(lastDay, -1); return justDay(lastDay); }
/** * Returns a {@link DateRange} starting on first day of month in which the specified date is and ending on last day of that * month. * * @param date date from which to get the month date range * @return {@link DateRange} representing the month in which the specified date is */ @SuppressWarnings("deprecation") public static DateRange getMonthDateRange(Date date) { Date startDate = new Date(date.getTime()); CalendarUtil.setToFirstDayOfMonth(startDate); // the above method will set hours to 12 startDate.setHours(0); Date endDate = new Date(date.getTime()); CalendarUtil.setToFirstDayOfMonth(endDate); CalendarUtil.addMonthsToDate(endDate, 1); CalendarUtil.addDaysToDate(endDate, -1); endDate.setHours(0); return new DateRange(startDate, endDate, CalendarUtil.getDaysBetween(startDate, endDate)); }
/** * Constructor. */ public CalendarModel() { currentMonth = new Date(); CalendarUtil.setToFirstDayOfMonth(currentMonth); // Finding day of week names Date date = new Date(); for (int i = 1; i <= DAYS_IN_WEEK; i++) { date.setDate(i); int dayOfWeek = date.getDay(); dayOfWeekNames[dayOfWeek] = getDayOfWeekFormatter().format(date); } // Finding day of month names date.setMonth(0); for (int i = 1; i < MAX_DAYS_IN_MONTH; ++i) { date.setDate(i); dayOfMonthNames[i] = getDayOfMonthFormatter().format(date); } // finding month names date.setDate(1); for (int i = 0; i < MONTHS_IN_YEAR; ++i) { date.setMonth(i); monthOfYearNames[i] = getMonthFormatter().format(date); } }
/** * Constructor. */ public CalendarModel() { currentMonth = new Date(); CalendarUtil.setToFirstDayOfMonth(currentMonth); // Finding day of week names Date date = new Date(); for (int i = 1; i <= DAYS_IN_WEEK; i++) { date.setDate(i); int dayOfWeek = date.getDay(); dayOfWeekNames[dayOfWeek] = getDayOfWeekFormatter().format(date); } // Finding day of month names date.setMonth(0); for (int i = 1; i < MAX_DAYS_IN_MONTH; ++i) { date.setDate(i); dayOfMonthNames[i] = getDayOfMonthFormatter().format(date); } // finding month names date.setDate(1); for (int i = 0; i < MONTHS_IN_YEAR; ++i) { date.setMonth(i); monthOfYearNames[i] = getMonthFormatter().format(date); } }