Java Utililty Methods Date to Month

List of utility methods to do Date to Month

Description

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

Method

intgetMonthInterval(Date _one, Date _two)
get Month Interval
Calendar one = Calendar.getInstance();
one.setTime(_one);
Calendar two = Calendar.getInstance();
two.setTime(_two);
int yearInt = two.get(Calendar.YEAR) - one.get(Calendar.YEAR);
int monthInt = two.get(Calendar.MONTH) - one.get(Calendar.MONTH);
int dayInt = two.get(Calendar.DAY_OF_MONTH) - one.get(Calendar.DAY_OF_MONTH);
return yearInt * 12 + monthInt + (dayInt > 0 ? 1 : 0);
...
DategetMonthLastDate(Date date)
get Month Last Date
cal.setTime(date);
cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
Date tmpdate1 = new Date(cal.getTime().getTime());
return tmpdate1;
DategetMonthLastDay(Date date)
get Month Last Day
Calendar calendar = Calendar.getInstance();
if (null != date) {
    calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
...
StringgetMonthlyKey(Date d)
get Monthly Key
Calendar c = Calendar.getInstance();
c.setTime(d);
int month = c.get(Calendar.MONTH) + 1;
return c.get(Calendar.YEAR) + "_" + (month < 10 ? "0" : "") + month;
StringgetMonthName(Date date)
get Month Name
Calendar temp = Calendar.getInstance();
temp.setTime(date);
int y = temp.get(Calendar.YEAR);
int m = temp.get(Calendar.MONTH) + 1;
int d = temp.get(Calendar.DAY_OF_MONTH);
int rm = 0, ry = 0;
if (d >= 1 && d <= 20) {
    rm = m;
...
IntegergetMonthOfCurrentTime(Date date)
get Month Of Current Time
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
intgetMonthOffset(Date sourceDate, Date targetDate)
get Month Offset
int sourceMonth = getMonth(sourceDate);
int sourceYear = getYear(sourceDate);
int targetMonth = getMonth(targetDate);
int targetYear = getYear(targetDate);
return (sourceYear - targetYear) * 12 + sourceMonth - targetMonth;
intgetMonthOfThisYear(Date currentdate)
get Month Of This Year
Calendar cal = Calendar.getInstance();
if (currentdate != null) {
    cal.setTime(currentdate);
} else {
    cal.setTime(new Date());
return cal.get(Calendar.MONTH) + 1;
intgetMonthOfYear(Date date)
get Month Of Year
return getField(date, Calendar.MONTH);
intgetMonthOfYear(Date dt)
Get and return the month of year from the given Date dt
Calendar cal = new GregorianCalendar();
cal.setTime(dt);
return (cal.get(Calendar.MONTH));