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

public static long formatTimeString(String timeString) {
    long time = 0;
    if (timeString == null || timeString.length() < 1) {
        return time;
    }/* www . ja  v a  2s.  c o m*/
    try {
        String[] array = timeString.split(DASH);
        int year = Integer.valueOf(array[0]);
        int month = Integer.valueOf(array[1]);
        int day = Integer.valueOf(array[2]);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);

        time = calendar.getTimeInMillis();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return time;
}

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   w ww.ja  v a 2 s .  com
 * @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

public static Date getFirstDayOfMonth(Date date) {
    if (date == null) {
        return null;
    }/*from   w  w  w  .j a v  a2 s .c  o  m*/
    Calendar calendar = Calendar.getInstance(Locale.getDefault());
    calendar.setTime(date);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    return calendar.getTime();
}

From source file:Main.java

public static String formatDateDiff(Calendar fromDate, Calendar toDate) {
    boolean future = false;
    if (toDate.equals(fromDate)) {
        return ("now");
    }/*  w w  w. j a v a 2  s  .  c  om*/
    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 int[] getSchoolCalDate(int year, int month, int day, int week, int dayOfWeek) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month - 1, day);/*from   ww w.  ja  va 2s  .  co m*/
    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 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 getDayOfMonth(long date) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(date);//  ww w. j a  v a 2s  . co  m
    return c.get(Calendar.DAY_OF_MONTH);
}

From source file:MainClass.java

public static int countDiffDay(Calendar c1, Calendar c2) {
    int returnInt = 0;
    while (!c1.after(c2)) {
        c1.add(Calendar.DAY_OF_MONTH, 1);
        returnInt++;//from  ww w. j ava 2 s  .c  o m
    }

    if (returnInt > 0) {
        returnInt = returnInt - 1;
    }

    return (returnInt);
}

From source file:Main.java

public static void moveToCalendarDay(Calendar cal, int day) {
    assertArgumentNotMinusNotZeroInteger("day", day);
    cal.set(Calendar.DAY_OF_MONTH, day);
}

From source file:Main.java

/** Convert a calendar date into a string as specified by http://tools.ietf.org/html/rfc822#section-5.1
 * @param date the calendar instance to convert to a string
 * @param locale the locale to use when outputting strings such as a day of the week, or month of the year.
 * @return a string in the format: "day_abbreviation, day_of_month month_abbreviation year hour:minute:second GMT"
 *///  w ww  . jav a  2 s .  c  o  m
// package-private - unused
static final String convertDateToStringRfc822(Calendar date, Locale locale) {
    Date a = Date.from(Instant.now());
    a.toString();
    int day = date.get(Calendar.DAY_OF_MONTH);
    int hour = date.get(Calendar.HOUR_OF_DAY);
    int minute = date.get(Calendar.MINUTE);
    int second = date.get(Calendar.SECOND);
    String str = date.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, locale) + ", "
            + (day < 10 ? "0" + day : day) + " " + date.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale)
            + " " + date.get(Calendar.YEAR) + " " + (hour < 10 ? "0" + hour : hour) + ":"
            + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second) + " GMT";
    return str;
}