Example usage for java.util Calendar MONTH

List of usage examples for java.util Calendar MONTH

Introduction

In this page you can find the example usage for java.util Calendar MONTH.

Prototype

int MONTH

To view the source code for java.util Calendar MONTH.

Click Source Link

Document

Field number for get and set indicating the month.

Usage

From source file:Main.java

public static int monthsBetweenDates(String start, String end) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("MMMM, yyyy", Locale.getDefault());
    Date startDate = format.parse(start);
    Date endDate = format.parse(end);

    Calendar startCalendar = new GregorianCalendar();
    startCalendar.setTime(startDate);//from w w  w  .j ava2 s .  c  o  m
    Calendar endCalendar = new GregorianCalendar();
    endCalendar.setTime(endDate);

    int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
    return diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
}

From source file:Main.java

public static void moveToCalendarMonth(Calendar cal, int month) {
    assertArgumentNotMinusNotZeroInteger("month", month);
    cal.set(Calendar.MONTH, month - 1); // zero origin headache
}

From source file:Main.java

/**
 * Get last month end day/*from w w w. jav  a2 s  .  c  o m*/
 */
public static int getLastMonthEndDay() {
    Calendar lastDate = Calendar.getInstance();
    lastDate.add(Calendar.MONTH, -1);
    lastDate.set(Calendar.DATE, 1);
    lastDate.roll(Calendar.DATE, -1);
    return lastDate.getActualMaximum(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

/**
 * Get next month first day/*from   www. java2  s. co  m*/
 */
public static int getNextMonthFirstDay() {
    Calendar lastDate = Calendar.getInstance();
    lastDate.add(Calendar.MONTH, 1);
    lastDate.set(Calendar.DATE, 1);
    return lastDate.getActualMinimum(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static void trunkYear(Calendar c) {
    trunkMonth(c);
    c.set(Calendar.MONTH, 0);
}

From source file:Main.java

/**
 * Get next month end day/*  ww  w  .  ja  v  a  2 s.com*/
 *
 * @return
 */
public static int getNextMonthEndDay() {
    Calendar lastDate = Calendar.getInstance();
    lastDate.add(Calendar.MONTH, 1);
    lastDate.set(Calendar.DATE, 1);
    lastDate.roll(Calendar.DATE, -1);
    return lastDate.getActualMaximum(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

/**
 * Get the age of the user. Takes in their birthday and calculates it according to today's date
 * @param birthday Date Object//  w ww  .j a  v a 2 s .  c  om
 * @return Returns an int of their age (IE 20, 55, 18). If the date is in the future, it will
 *         return -1 instead.
 */
public static int getAge(Date birthday) {

    Calendar now = Calendar.getInstance();
    Calendar dob = Calendar.getInstance();
    dob.setTime(birthday);

    //First check for in the future:
    if (dob.after(now)) {
        return -1;
    }

    int year1 = now.get(Calendar.YEAR);
    int year2 = dob.get(Calendar.YEAR);

    int age = year1 - year2;

    int month1 = now.get(Calendar.MONTH);
    int month2 = dob.get(Calendar.MONTH);

    if (month2 > month1) {
        age--;

    } else if (month1 == month2) {
        int day1 = now.get(Calendar.DAY_OF_MONTH);
        int day2 = dob.get(Calendar.DAY_OF_MONTH);
        if (day2 > day1) {
            age--;
        }
    }

    return age;
}

From source file:Main.java

/**
 * Get last month first day//  w  w w .j a  v a 2  s  .  c  o  m
 */
public static int getLastMonthFirstDay() {
    Calendar lastDate = Calendar.getInstance();
    lastDate.set(Calendar.DATE, 1);
    lastDate.add(Calendar.MONTH, -1);
    return lastDate.getActualMinimum(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static String getCurrMonthAfter(int monthDiff, String format) {
    SimpleDateFormat ft = new SimpleDateFormat(format);
    Calendar c = new GregorianCalendar();
    c.add(Calendar.MONTH, monthDiff);
    return ft.format(c.getTime());
}

From source file:Main.java

/**
 * Checks the calendar is today./*www  .ja va 2 s .  c  om*/
 * @param calendar Calendar object to check.
 * @return true if calendar is today else false.
 */
public static boolean isToday(Calendar calendar) {
    return calendar.get(Calendar.MONTH) == Calendar.getInstance().get(Calendar.MONTH)
            && calendar.get(Calendar.DAY_OF_MONTH) == Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}