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

MapgetMonthsMap()
Returns a map of months
return monthNames;
intgetMonthsOfAge(Date birth, Date now)
get Months Of Age
Calendar nowCalendar = new GregorianCalendar();
Calendar birthCalendar = new GregorianCalendar();
birthCalendar.setTime(birth);
nowCalendar.setTime(now);
if (nowCalendar.getTimeInMillis() > birthCalendar.getTimeInMillis()) {
    int months = nowCalendar.get(Calendar.MONTH) - birthCalendar.get(Calendar.MONTH);
    int yearDiff = nowCalendar.get(Calendar.YEAR) - birthCalendar.get(Calendar.YEAR);
    if (yearDiff > 0) {
...
intgetMonthSpan(Date begin, Date end)
get Month Span
Calendar beginCal = new GregorianCalendar();
beginCal.setTime(begin);
Calendar endCal = new GregorianCalendar();
endCal.setTime(end);
int m = (endCal.get(Calendar.MONTH)) - (beginCal.get(Calendar.MONTH));
int y = (endCal.get(Calendar.YEAR)) - (beginCal.get(Calendar.YEAR));
return y * 12 + m;
longgetNextMonth(long date)
Returns the next month.
return incrementMonth(date, 1);
DategetNextMonthExtention(Date dt, Long n)
get Next Month Extention
Calendar cal = new GregorianCalendar();
cal.setTime(dt);
Calendar firstCal = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue(),
        1);
if (firstCal.getActualMaximum(Calendar.DAY_OF_MONTH) < cal.get(Calendar.DAY_OF_MONTH)) {
    return new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + n.intValue() + 1, 1)
            .getTime();
} else {
...
StringgetNowMonth()
get Now Month
Date date = new Date();
int year = date.getYear() + 1900;
int month = date.getMonth() + 1;
return year + "-" + (month < 10 ? "0" : "") + month;
CalendargetStartOfMonth(Calendar scal)
Converts a calendar to the first day of the month
scal.add(Calendar.DAY_OF_MONTH, -(scal.get(Calendar.DAY_OF_MONTH) - 1));
return scal;
StringgetThisMonthEnd()
get This Month End
String strY = null;
String strZ = null;
boolean leap = false;
Calendar localTime = Calendar.getInstance();
int x = localTime.get(Calendar.YEAR);
int y = localTime.get(Calendar.MONTH) + 1;
if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
    strZ = "31";
...
StringgetThisMonthStart()
get This Month Start
String strY = null;
Calendar localTime = Calendar.getInstance();
int x = localTime.get(Calendar.YEAR);
int y = localTime.get(Calendar.MONTH) + 1;
strY = y >= 10 ? String.valueOf(y) : ("0" + y);
return x + "-" + strY + "-01";
longincrementMonth(long date, int increment)
increment Month
Calendar calendar = CALENDAR;
synchronized (calendar) {
    calendar.setTimeInMillis(date);
    calendar.add(Calendar.MONTH, increment);
    return calendar.getTimeInMillis();