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 boolean isSameDateInCalendar(Calendar calendar, Date date) {
    Calendar dateCalendar = Calendar.getInstance();
    dateCalendar.setTime(date);/* w  w w .j  a v  a  2  s.  c  o m*/
    return dateCalendar.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)
            && dateCalendar.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)
            && dateCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR);
}

From source file:com.googlecode.commons.swing.util.DateUtils2.java

public static Date getStartOfMonth(Date month) {
    if (month == null) {
        return null;
    }//from   w  w w .j av  a2 s.  com
    Date start = (Date) month.clone();
    start = DateUtils.truncate(start, Calendar.MONTH);
    return start;
}

From source file:Main.java

public static int getMonth(String strDate, String format) {
    int month = 0;
    try {//  w ww  . j  a va2s  . c o  m
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(mSimpleDateFormat.parse(strDate));
        month = c.get(Calendar.MONTH) + 1;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return month;
}

From source file:Main.java

public static int[] getTimeFields(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);//from w ww . j a v  a2  s . c  o m
    int[] timeFields = new int[6];
    timeFields[0] = calendar.get(Calendar.YEAR);
    timeFields[1] = calendar.get(Calendar.MONTH);
    timeFields[2] = calendar.get(Calendar.DAY_OF_MONTH);
    timeFields[3] = calendar.get(Calendar.HOUR_OF_DAY);
    timeFields[4] = calendar.get(Calendar.MINUTE);
    timeFields[5] = calendar.get(Calendar.SECOND);
    return timeFields;
}

From source file:Main.java

/**
 * Converts input time from Java to DOS format
 * @param time//from w  w w . j a v a2 s.c  om
 * @return time in DOS format 
 */
public static long javaToDosTime(long time) {

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);

    int year = cal.get(Calendar.YEAR);
    if (year < 1980) {
        return (1 << 21) | (1 << 16);
    }
    return (year - 1980) << 25 | (cal.get(Calendar.MONTH) + 1) << 21 | cal.get(Calendar.DATE) << 16
            | cal.get(Calendar.HOUR_OF_DAY) << 11 | cal.get(Calendar.MINUTE) << 5
            | cal.get(Calendar.SECOND) >> 1;
}

From source file:Main.java

public static Date startOfThisMonth(int startDayOfMonth) {
    Calendar cal = Calendar.getInstance();

    if (cal.get(Calendar.DATE) < startDayOfMonth) // the start is startDayOfMonth, last month
        cal.add(Calendar.MONTH, -1);

    int lastDayInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    if (startDayOfMonth <= lastDayInMonth)
        cal.set(Calendar.DATE, startDayOfMonth);
    else {/*w w  w .  j av a2s. c  om*/
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DATE, 1);
    }

    return startOfDay(cal.getTime());
}

From source file:Main.java

/**
 * @param date//from   w  w  w .  j a  v  a 2s . c  o m
 * @return Returns month of the argument date. The returned values is based
 *         on zero-index i.e. for month January, the values returned is "0"
 */
private static String getMonthFromDate(Date date) {
    if (date == null) {
        return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return String.valueOf(cal.get(Calendar.MONTH));
}

From source file:Main.java

public static Date startOfNextMonth(int startDayOfMonth) {
    Calendar cal = Calendar.getInstance();

    if (cal.get(Calendar.DATE) >= startDayOfMonth)// the end is one day past startDayOfMonth, next month
        cal.add(Calendar.MONTH, 1);

    int lastDayInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    if (startDayOfMonth < lastDayInMonth)
        cal.set(Calendar.DATE, startDayOfMonth + 1);
    else {//  w  w  w  .  j av a 2 s.  c  o m
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DATE, 1);
    }

    return startOfDay(cal.getTime());
}

From source file:Main.java

/**
 * Add date time with five years.//from   w ww  .j a  v  a2 s  .com
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithFiveYears(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.MONTH, isPast ? -10 : 10);
    calendar.add(Calendar.YEAR, isPast ? -5 : 5);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

/**
 * Formats a date/time value as a string.
 * //from  www  . j  ava  2  s .  c  o  m
 * @param date the date to format
 * @return the formatted date
 */
public static String formatDateTime(Calendar date) {
    StringBuilder builder = new StringBuilder(16);
    appendField(builder, date.get(Calendar.YEAR), 4);
    appendField(builder, date.get(Calendar.MONTH) + 1, 2);
    appendField(builder, date.get(Calendar.DAY_OF_MONTH), 2);
    appendField(builder, date.get(Calendar.HOUR_OF_DAY), 2);
    appendField(builder, date.get(Calendar.MINUTE), 2);
    appendField(builder, date.get(Calendar.SECOND), 2);
    return builder.toString();
}