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 XMLGregorianCalendar convertDate(Date date) {
    try {//w  ww. ja  v  a2 s  .  c o  m
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
        xmlGregorianCalendar.setDay(c.get(Calendar.DAY_OF_MONTH));
        xmlGregorianCalendar.setMonth(c.get(Calendar.MONTH));
        xmlGregorianCalendar.setYear(c.get(Calendar.YEAR));
        return xmlGregorianCalendar;
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * yyyy-MM-dd HH:mm:ss/*from   ww  w  .j  a  v a2 s. c  om*/
 */
public static String getFormatTime1(Calendar c) {
    if (null == c) {
        return "null";
    }
    DecimalFormat df = new DecimalFormat("00");
    String strCurrTime = c.get(Calendar.YEAR) + "-" + df.format((c.get(Calendar.MONTH) + 1)) + "-"
            + df.format(c.get(Calendar.DAY_OF_MONTH)) + " " + df.format(c.get(Calendar.HOUR_OF_DAY)) + ":"
            + df.format(c.get(Calendar.MINUTE)) + ":" + df.format(c.get(Calendar.SECOND));

    return strCurrTime;
}

From source file:Main.java

/**
 * calculate day count/* w  w  w. j av a2  s  .  c o  m*/
 * @param startYear start year
 * @param startMonth start month
 * @param startDay start day
 * @param endYear end year
 * @param endMonth end month
 * @param endDay end day
 * @return day count
 */
public static int countDays(int startYear, int startMonth, int startDay, int endYear, int endMonth,
        int endDay) {
    Calendar startC = Calendar.getInstance();
    startC.set(Calendar.YEAR, startYear);
    startC.set(Calendar.MONTH, startMonth - 1);
    startC.set(Calendar.DAY_OF_MONTH, startDay);
    Calendar endC = Calendar.getInstance();
    endC.set(Calendar.YEAR, endYear);
    endC.set(Calendar.MONTH, endMonth - 1);
    endC.set(Calendar.DAY_OF_MONTH, endDay);
    return (int) ((endC.getTimeInMillis() - startC.getTimeInMillis()) / 86400000 + 1);
}

From source file:Main.java

public static int getMonthOfYear(long date) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(date);/* www.  j  a  v a 2s  .co m*/
    return c.get(Calendar.MONTH);
}

From source file:Main.java

/**
 * Returns the year (index 0), month (index 1) and day (index 2) of the given timestamp.
 * @param timestamp the timestamp/*from www.ja v a  2 s  . c  o  m*/
 * @return the year (index 0), month (index 1) and day (index 2) of the given timestamp
 */
public static int[] getDayMonthYear(long timestamp) {
    GregorianCalendar gregorianCalendar = new GregorianCalendar();
    gregorianCalendar.setTimeInMillis(timestamp);

    int day = gregorianCalendar.get(Calendar.DAY_OF_MONTH);
    int month = gregorianCalendar.get(Calendar.MONTH) + 1;
    int year = gregorianCalendar.get(Calendar.YEAR);

    return new int[] { year, month, day };
}

From source file:Main.java

/**
 * Check if date1 is after date2 or not//from   w  w  w  .j av  a  2  s.  c o  m
 * @return true if date1 after date 2, otherwise false.
 */
public static boolean isBefore(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);

    if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) {
        return true;
    } else if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.MONTH) < cal2.get(Calendar.MONTH)) {
        return true;
    } else if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
            && cal1.get(Calendar.DAY_OF_MONTH) < cal2.get(Calendar.DAY_OF_MONTH)) {
        return true;
    }

    return false;
}

From source file:Main.java

public static int[] splitDate(Calendar calendar) {
    return new int[] { calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND) };
}

From source file:Main.java

/**
 * if two date is the same day, for example ,1999-11-21 00:00:00 and  1999-11-21 23:15:40 are in same day,
 * but 1999-11-21 00:00:00 and 2010-11-21 00:00:00 are not
 *
 * @param ltime/*from ww  w . j ava 2s .  co  m*/
 * @param rtime
 * @return
 */
public static boolean sameDay(long ltime, long rtime) {
    Calendar l = Calendar.getInstance();
    Calendar r = Calendar.getInstance();
    l.setTimeInMillis(ltime);
    r.setTimeInMillis(rtime);

    return l.get(Calendar.YEAR) == r.get(Calendar.YEAR) && l.get(Calendar.MONTH) == r.get(Calendar.MONTH)
            && l.get(Calendar.DAY_OF_MONTH) == r.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

/**
 * get month between minTime and maxTime, including head and tail
 *
 * @param minTime//from w ww. ja  v a  2s.  co m
 * @param maxTime
 * @return
 */
public static int getMonthNum(long minTime, long maxTime) {
    Calendar min = Calendar.getInstance();
    min.setTimeInMillis(minTime);
    Calendar max = Calendar.getInstance();
    max.setTimeInMillis(maxTime);
    if (max.before(min)) {
        throw new IllegalArgumentException("max date is before min date");
    }
    int minMonth = min.get(Calendar.MONTH) + 1;
    int maxMonth = max.get(Calendar.MONTH) + 1;
    return (max.get(Calendar.YEAR) - min.get(Calendar.YEAR)) * 12 + maxMonth - minMonth + 1;
}

From source file:Main.java

public static String getMsgDate(long msg_time) {
    Calendar cl = Calendar.getInstance();
    cl.setTimeInMillis(msg_time); //here your time in miliseconds
    String date = "";

    String[] monthNames = { "January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December" };

    SimpleDateFormat month_date = new SimpleDateFormat("MMM");
    String month_name = monthNames[cl.get(Calendar.MONTH)];

    int day = cl.get(Calendar.DAY_OF_MONTH);
    int year = cl.get(Calendar.YEAR);

    int current_year = Calendar.getInstance().get(Calendar.YEAR);
    if (current_year == year) {
        date = month_name + " " + day;
    } else {/*ww w .jav  a 2 s  .  c o  m*/
        date = month_name + " " + day + ", " + year;
    }

    return date;
}