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 getCurrentTimeStampDD_MM_HH_MM() {
    Calendar cal = new GregorianCalendar();
    return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
            + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + ". "
            + makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
            + makeTimeString(cal.get(Calendar.MINUTE), 2);
}

From source file:Main.java

public static String getCurrentDateString(Calendar calendar) {
    if (calendar == null)
        return "";
    return calendar.get(Calendar.YEAR) + "-" + String.format("%02d", (calendar.get(Calendar.MONTH) + 1)) + "-"
            + String.format("%02d", calendar.get(Calendar.DATE));
}

From source file:Main.java

public static String getTimeStamp(long l) {
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(l);/*from w ww .  j  a va 2 s.  c  o m*/
    return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
            + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + "." + makeTimeString(cal.get(Calendar.YEAR), 4)
            + " " + 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

private static String formatPeriodDate(int daysPeriod) {
    Calendar now = Calendar.getInstance();
    Calendar period = Calendar.getInstance();
    period.add(Calendar.DAY_OF_MONTH, daysPeriod);
    SimpleDateFormat sdf = new SimpleDateFormat("MMMM", Locale.US);

    if (now.get(Calendar.YEAR) == period.get(Calendar.YEAR)) {
        if (now.get(Calendar.MONTH) == period.get(Calendar.MONTH)) {
            return period.get(Calendar.DAY_OF_MONTH) + "-" + now.get(Calendar.DAY_OF_MONTH) + " "
                    + sdf.format(now.getTime()) + ", " + now.get(Calendar.YEAR);
        }/*ww  w . j  a v  a 2 s  .  c o  m*/
        return period.get(Calendar.DAY_OF_MONTH) + " " + sdf.format(period.getTime()) + " - "
                + now.get(Calendar.DAY_OF_MONTH) + " " + sdf.format(now.getTime()) + ", "
                + now.get(Calendar.YEAR);
    }
    return period.get(Calendar.DAY_OF_MONTH) + " " + sdf.format(period.getTime()) + " "
            + period.get(Calendar.YEAR) + " - " + now.get(Calendar.DAY_OF_MONTH) + " "
            + sdf.format(now.getTime()) + " " + now.get(Calendar.YEAR);
}

From source file:Main.java

/**
 * yyyyMMddHHmmss//www .j  av  a 2s .c  o  m
 */
public static String getFormatTime3(Calendar c) {
    DecimalFormat df = new DecimalFormat("00");
    String strFileName = 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 strFileName;
}

From source file:Main.java

public static String getTimeStampDDMMYYYY(long l) {
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(l);/*from  w ww .j  a v  a  2  s.  c  o m*/
    return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
            + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + "." + makeTimeString(cal.get(Calendar.YEAR), 4);
}

From source file:Main.java

public static int getMonthFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;/*from   w w  w  .  ja  v a  2s. co  m*/
    int month = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        month = cal.get(Calendar.MONTH);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return month;
}

From source file:Main.java

public static int getAge(String year, String month, String day) throws NumberFormatException {
    int yearInt = Integer.parseInt(year);
    int monthInt = Integer.parseInt(month);
    int dayInt = Integer.parseInt(day);
    monthInt--;// w w w . j  a  va2  s  . c  o m
    Calendar birthCal = Calendar.getInstance();
    birthCal.set(yearInt, monthInt, dayInt);
    Calendar todayCal = Calendar.getInstance();

    int diff = todayCal.get(Calendar.YEAR) - birthCal.get(Calendar.YEAR);
    if (birthCal.get(Calendar.MONTH) > todayCal.get(Calendar.MONTH)
            || (birthCal.get(Calendar.MONTH) == todayCal.get(Calendar.MONTH)
                    && birthCal.get(Calendar.DATE) > todayCal.get(Calendar.DATE))) {
        diff--;
    }
    return diff;
}

From source file:Main.java

public static boolean areSameDays(long time1, long time2) {
    Calendar calendar = calendarThreadLocal.get();
    if (calendar == null) {
        calendar = Calendar.getInstance();
        calendarThreadLocal.set(calendar);
    }// w  w  w  .ja  v a  2 s  .  c o m
    calendar.setTimeInMillis(time1 * 1000L);
    int y1 = calendar.get(Calendar.YEAR);
    int m1 = calendar.get(Calendar.MONTH);
    int d1 = calendar.get(Calendar.DATE);
    calendar.setTimeInMillis(time2 * 1000L);
    int y2 = calendar.get(Calendar.YEAR);
    int m2 = calendar.get(Calendar.MONTH);
    int d2 = calendar.get(Calendar.DATE);

    return y1 == y2 && m1 == m2 && d1 == d2;
    // return (time1 / (60 * 60 * 24)) == (time2 / (60 * 60 * 24));
}

From source file:Main.java

/**
 * Calculates the difference in days, rounding the operands down to the nearest day.
 * Ex. Jan 3rd 12:31 pm - Jan 2nd 4:00 pm
 * =   Jan 3rd - Jan 2nd/*from  w w  w  .j av  a2s.c  o  m*/
 * =   1 day 
 * @return The difference in days.
 */
public static int differenceInDays(GregorianCalendar minuend, GregorianCalendar subtrahend) {
    GregorianCalendar minuendFloor = new GregorianCalendar(minuend.get(Calendar.YEAR),
            minuend.get(Calendar.MONTH), minuend.get(Calendar.DAY_OF_MONTH));
    GregorianCalendar subtrahendFloor = new GregorianCalendar(subtrahend.get(Calendar.YEAR),
            subtrahend.get(Calendar.MONTH), subtrahend.get(Calendar.DAY_OF_MONTH));
    GregorianCalendar result = new GregorianCalendar();
    result.setTimeInMillis(minuendFloor.getTimeInMillis() - subtrahendFloor.getTimeInMillis());
    return result.get(Calendar.DAY_OF_YEAR);
}