Example usage for java.util Calendar DAY_OF_MONTH

List of usage examples for java.util Calendar DAY_OF_MONTH

Introduction

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

Prototype

int DAY_OF_MONTH

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

Click Source Link

Document

Field number for get and set indicating the day of the month.

Usage

From source file:Main.java

/**
 * Return if the two calendar is in the same day, ignoring time
 * @param day1// w  w  w.j av a  2s.c o m
 * @param day2
 * @return
 */
public static boolean isSameDay(Calendar day1, Calendar day2) {
    return day1.get(Calendar.YEAR) == day2.get(Calendar.YEAR)
            && day1.get(Calendar.MONTH) == day2.get(Calendar.MONTH)
            && day1.get(Calendar.DAY_OF_MONTH) == day2.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static int getUserAge(Date birthDay) {
    Calendar cal = Calendar.getInstance();

    if (cal.before(birthDay)) {
        return 0;
    }/*  ww  w.j  a  v  a  2  s . co  m*/
    int yearNow = cal.get(Calendar.YEAR);
    int monthNow = cal.get(Calendar.MONTH);
    int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
    if (null == birthDay) {
        return 0;
    } else {
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                } else {
                    // do nothing
                }
            } else {
                // monthNow>monthBirth
                age--;
            }
        } else {
            // monthNow<monthBirth
            // donothing
        }
        return age;
    }
}

From source file:Main.java

/**
 * returns 1 if calendar1 > calendar2 0 if calendar1 = calendar2 (same day
 * only) -1 if calendar1 > calendar2//  w  ww  . j a v  a  2  s .  c  om
 * 
 * @param calendar1
 * @param calendar2
 */
public static byte compareOnlyDay(Calendar calendar1, Calendar calendar2) {
    byte dayBefore = 0;
    int findDiff = 0;//

    findDiff = calendar1.get(Calendar.DAY_OF_MONTH) - calendar2.get(Calendar.DAY_OF_MONTH);
    if (findDiff > 0) {
        dayBefore = 1;
    } else if (findDiff < 0) {
        dayBefore = -1;
    } else {
        dayBefore = 0;
    }

    return dayBefore;
}

From source file:Main.java

/**
 * @param days//  ww w.  j a  va  2 s  .c  o m
 *            number of days after September 02, 1975 (what happened on this
 *            day?)
 * @return
 */
public static Calendar getDayFromQuickLogEntry(int days) {
    Calendar logDay = GregorianCalendar.getInstance();
    logDay.set(Calendar.DAY_OF_MONTH, 2);
    logDay.set(Calendar.MONTH, Calendar.SEPTEMBER);
    logDay.set(Calendar.YEAR, 1975);
    logDay.add(Calendar.DAY_OF_YEAR, days);
    return logDay;
}

From source file:Main.java

/**
 * set to the last day of month//from   ww  w.  jav  a 2 s .  com
 * @param calendar
 */
public static void setToLastDay(Calendar calendar) {
    int year = getYear(calendar);
    int month = getMonth(calendar);
    int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    calendar.clear();
    calendar.set(year, month, maxDay);
    calendar.getTimeInMillis();
}

From source file:Main.java

public static List<String> getOldWeekDays() {
    final Calendar c = Calendar.getInstance();
    String[] months = new String[8];
    for (int i = 0; i < 8; i++) {
        months[i] = new SimpleDateFormat("MM.dd").format(new Date(c.getTimeInMillis()));
        c.add(Calendar.DAY_OF_MONTH, -1);
    }//from   www  . j av a  2s  . com
    return Arrays.asList(months);
}

From source file:Main.java

public static String getCurrentTimeStamp() {
    Calendar cal = new GregorianCalendar();
    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

public static String getCurrentTimeStampDD_MM_YYYY() {
    Calendar cal = new GregorianCalendar();
    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 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

/**
 * Returns a date formatted in ANSI SQL.
 *
 * @param date the Date.//from   w w  w .  j a va  2 s.  c  o  m
 * @return a date String.
 */
public static String getSqlDateString(Date date) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(date);

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

    String yearString = String.valueOf(year);
    String monthString = month < 10 ? "0" + month : String.valueOf(month);
    String dayString = day < 10 ? "0" + day : String.valueOf(day);

    return yearString + "-" + monthString + "-" + dayString;
}