Java Utililty Methods Month Calculate

List of utility methods to do Month Calculate

Description

The list of methods to do Month Calculate are organized into topic(s).

Method

CalendaralignMonth(Calendar timestamp)
Aligns the time fields to the start of the month.
timestamp.set(Calendar.DAY_OF_MONTH, 1);
return alignDay(timestamp);
longbeforeAMonth()
before A Month
Calendar cal = Calendar.getInstance();
Date currentDate = new Date();
cal.setTime(currentDate);
cal.add(Calendar.MONTH, -1);
Date date = cal.getTime();
return date.getTime();
intcalendarMonthToInt(int calendarMonth)
calendar Month To Int
if (calendarMonth == Calendar.JANUARY) {
    return 1;
} else if (calendarMonth == Calendar.FEBRUARY) {
    return 2;
} else if (calendarMonth == Calendar.MARCH) {
    return 3;
} else if (calendarMonth == Calendar.APRIL) {
    return 4;
...
intcurrentMonth()
current Month
Calendar rightNow = Calendar.getInstance();
return rightNow.get(Calendar.MONTH);
ListdivideIntoMonthlyIntervals(Date start, Date end)
returns a list of monthly intervals for the first of each month the first entry in the list is the 1st of the month before start and the last entry is the 1st of the month after the end.
List<Date> result = new ArrayList<Date>();
Calendar c = startOfMonth(start);
result.add(c.getTime());
do {
    int mm = c.get(Calendar.MONTH);
    int yy = c.get(Calendar.YEAR);
    mm++;
    if (mm >= 12) {
...
longendOfLastMonth()
end Of Last Month
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
cal.add(Calendar.DATE, -1);
Date date = cal.getTime();
...
intgetAgeInMonths(Date startDate, Date endDate)
get Age In Months
Calendar start = new GregorianCalendar();
start.setTime(startDate);
int startMOY = start.get(Calendar.MONTH);
int startYear = start.get(Calendar.YEAR);
Calendar end = new GregorianCalendar();
end.setTime(endDate);
int endMOY = end.get(Calendar.MONTH);
int endYear = end.get(Calendar.YEAR);
...
intgetAlphaMonth(String mon)
get Alpha Month
mon = mon.toLowerCase();
if (MONTHS.get(mon) != null) {
    return MONTHS.get(mon);
return 0;
IntegergetCurrentMonth()
get Current Month
return Calendar.getInstance().get(Calendar.MONTH) + 1;
intgetCurrentMonth()
get Current Month
return getCurrentDate().getMonth();