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 String formatDateDiff(Calendar fromDate, Calendar toDate) {
    boolean future = false;
    if (toDate.equals(fromDate)) {
        return ("now");
    }//  www.j  a  v  a2  s.  co  m
    if (toDate.after(fromDate)) {
        future = true;
    }
    StringBuilder sb = new StringBuilder();
    int[] types = new int[] { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY,
            Calendar.MINUTE, Calendar.SECOND };
    String[] names = new String[] { ("year"), ("years"), ("month"), ("months"), ("day"), ("days"), ("hour"),
            ("hours"), ("minute"), ("minutes"), ("second"), ("seconds") };
    int accuracy = 0;
    for (int i = 0; i < types.length; i++) {
        if (accuracy > 2) {
            break;
        }
        int diff = dateDiff(types[i], fromDate, toDate, future);
        if (diff > 0) {
            accuracy++;
            sb.append(" ").append(diff).append(" ").append(names[i * 2 + (diff > 1 ? 1 : 0)]);
        }
    }
    if (sb.length() == 0) {
        return "now";
    }
    return sb.toString().trim();
}

From source file:Main.java

public static String createJcFileName() {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());

    int y = c.get(Calendar.YEAR);
    int m = c.get(Calendar.MONTH);
    int d = c.get(Calendar.DAY_OF_MONTH);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    int second = c.get(Calendar.SECOND);
    return y + "-" + m + "-" + d + "-" + hour + "-" + minute + "-" + second;
}

From source file:Main.java

public static int[] getSchoolCalDate(int year, int month, int day, int week, int dayOfWeek) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month - 1, day);//from   w w w.j a  va 2s. c  om
    Log.d(TAG, "Date: " + cal.toString());
    Log.d(TAG, "year: " + year);
    Log.d(TAG, "month: " + month);
    Log.d(TAG, "day: " + day);
    cal.add(Calendar.DATE, -offset(cal.get(Calendar.DAY_OF_WEEK)));
    int count = (week - 1) * 7 + dayOfWeek - 1;
    Log.d(TAG, "count: " + count);
    cal.add(Calendar.DATE, count);
    int result[] = { cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH) };

    return result;
}

From source file:Main.java

public static void addCalendarMonth(Calendar cal, int months) {
    cal.add(Calendar.MONTH, months);
}

From source file:Main.java

static void setDate(Calendar cal, int month, int date, boolean endOfDay) {
    cal.clear();//from  w ww  .j  a v a 2 s. c om
    cal.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DATE, date);
    if (endOfDay) {
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
    } else {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }
}

From source file:Main.java

public static String date2String(Date date, boolean printTime) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);//from w w  w .j a v a 2  s  . c o m
    if (cal.get(Calendar.YEAR) > 10000) {
        return "---";
    }
    return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
            + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + "." + makeTimeString(cal.get(Calendar.YEAR), 4)
            + (printTime
                    ? " " + makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
                            + makeTimeString(cal.get(Calendar.MINUTE), 2) + ":"
                            + makeTimeString(cal.get(Calendar.SECOND), 2)
                    : "");
}

From source file:Main.java

public static int getSemester() {
    Calendar cal = Calendar.getInstance();

    int month = cal.get(Calendar.MONTH);

    if (month > Calendar.FEBRUARY && month < Calendar.SEPTEMBER)
        return SUMMER_SEMESTER;
    else/* ww w .  ja  va 2 s . c o m*/
        return WINTER_SEMESTER;
}

From source file:Main.java

/** Helper method - creates a Phoenix date <I>String</I> from a Date object.
   @param dteValue <I>Date</I> object to be converted.
   @return A <I>String</I> that follows the format "YYYY-MM-DDTHH:NN:SS.00000".
 *///  w ww. ja v a  2  s. com
public static String getXMLStringFromDate(Date dteValue) {
    if (null == dteValue)
        return null;

    Calendar pCalendar = Calendar.getInstance();

    pCalendar.setTime(dteValue);

    // Return the String value.
    // Special treatment for the month because the current implementation
    // values January as 0. Code below should work if that ever changes.
    return pCalendar.get(Calendar.YEAR) + "-" + padNumber(pCalendar.get(Calendar.MONTH) + 1 - Calendar.JANUARY)
            + "-" + padNumber(pCalendar.get(Calendar.DAY_OF_MONTH)) + "T"
            + padNumber(pCalendar.get(Calendar.HOUR_OF_DAY)) + ":" + padNumber(pCalendar.get(Calendar.MINUTE))
            + ":" + padNumber(pCalendar.get(Calendar.SECOND));
}

From source file:Main.java

public static int getItemsCounts(int periodType, long date) {
    switch (periodType) {
    case TYPE_DAY:
        return 24;

    case TYPE_WEEK:
        return 7;

    case TYPE_MONTH: {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date);/*  w  w  w. jav  a 2 s .com*/
        return c.getMaximum(Calendar.DAY_OF_MONTH);
    }

    case TYPE_YEAR: {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(date);
        return c.getMaximum(Calendar.MONTH) + 1;
    }
    }

    return 0;
}

From source file:Main.java

public static long getPeriodEnd(int periodType, long date) {
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(getPeriodStart(periodType, date));

    switch (periodType) {
    case TYPE_DAY: {
        cal.add(Calendar.DAY_OF_YEAR, 1);
        break;/*from w  ww  .  j a  va 2  s .  c  o m*/
    }

    case TYPE_WEEK: {
        cal.add(Calendar.WEEK_OF_YEAR, 1);
        break;
    }

    case TYPE_MONTH: {
        cal.add(Calendar.MONTH, 1);
        break;
    }

    case TYPE_YEAR: {
        cal.add(Calendar.YEAR, 1);
        break;
    }
    }

    cal.add(Calendar.MILLISECOND, -1);
    return cal.getTimeInMillis();
}