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

/**
 * Add date time with ten days.//  w ww .  j a v  a  2s  . c o m
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithTenDays(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.DAY_OF_MONTH, isPast ? -10 : 10);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

public static int[] getTimeFields(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);/*ww  w.  ja v a2  s .co  m*/
    int[] timeFields = new int[6];
    timeFields[0] = calendar.get(Calendar.YEAR);
    timeFields[1] = calendar.get(Calendar.MONTH);
    timeFields[2] = calendar.get(Calendar.DAY_OF_MONTH);
    timeFields[3] = calendar.get(Calendar.HOUR_OF_DAY);
    timeFields[4] = calendar.get(Calendar.MINUTE);
    timeFields[5] = calendar.get(Calendar.SECOND);
    return timeFields;
}

From source file:Main.java

/**
 * Add date time with almost one month.//from  w  w  w  . j  av a  2 s.c om
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithAlmostOneMonth(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.DAY_OF_MONTH, isPast ? -30 : 30);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

public static Date getMidnight(Date date) {
    Calendar day = Calendar.getInstance();
    day.setTime(date);//from   w  w w . j a v  a  2s  .co m
    day.set(Calendar.HOUR_OF_DAY, 0);
    day.set(Calendar.MINUTE, 0);
    day.set(Calendar.SECOND, 0);
    day.set(Calendar.MILLISECOND, 0);
    day.add(Calendar.DAY_OF_MONTH, 0);

    return day.getTime();
}

From source file:Main.java

public static String getOrdinalDayOfMonth(Calendar cal) {
    int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
    return String.valueOf(dayOfMonth) + getDayOfMonthSuffix(dayOfMonth);
}

From source file:Main.java

public static Date startOfThisMonth(int startDayOfMonth) {
    Calendar cal = Calendar.getInstance();

    if (cal.get(Calendar.DATE) < startDayOfMonth) // the start is startDayOfMonth, last month
        cal.add(Calendar.MONTH, -1);

    int lastDayInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    if (startDayOfMonth <= lastDayInMonth)
        cal.set(Calendar.DATE, startDayOfMonth);
    else {/* w w  w . ja  v  a  2  s. c o m*/
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DATE, 1);
    }

    return startOfDay(cal.getTime());
}

From source file:Main.java

private static void setEndOfMonthToCalendar(Calendar c) {
    // Get the First Day of next month so it will e considered until 0 hours
    // of next month
    int max = c.getMaximum(Calendar.DAY_OF_MONTH);
    c.set(Calendar.DAY_OF_MONTH, max);
    setEndOfDayToCalendar(c);//from ww  w  . j  av a  2 s .c  o  m
    Log.d("La Cuenta: ", "Max of Current Month: " + DateFormat.getInstance().format(c.getTime()));
}

From source file:Main.java

public static Date startOfNextMonth(int startDayOfMonth) {
    Calendar cal = Calendar.getInstance();

    if (cal.get(Calendar.DATE) >= startDayOfMonth)// the end is one day past startDayOfMonth, next month
        cal.add(Calendar.MONTH, 1);

    int lastDayInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    if (startDayOfMonth < lastDayInMonth)
        cal.set(Calendar.DATE, startDayOfMonth + 1);
    else {//from www . j a v  a 2 s.  co  m
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DATE, 1);
    }

    return startOfDay(cal.getTime());
}

From source file:Main.java

/**
 * Calculate the Julian Day for a given date using the following formula:
 * JD = 367 * Y - INT(7 * (Y + INT((M + 9)/12))/4) + INT(275 * M / 9)
 *      + D + 1721013.5 + UT/24//from   w  ww.  j  a v  a  2 s  .  c o m
 *
 * Note that this is only valid for the year range 1900 - 2099.
 */
public static double calculateJulianDay(Date date) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTime(date);

    double hour = cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) / 60.0f
            + cal.get(Calendar.SECOND) / 3600.0f;

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

    double jd = 367.0 * year - Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0)
            + Math.floor(275.0 * month / 9.0) + day + 1721013.5 + hour / 24.0;
    return jd;
}

From source file:Main.java

/**
 * Formats a date/time value as a string.
 * // w w w . j  av  a2  s  .c om
 * @param date the date to format
 * @return the formatted date
 */
public static String formatDateTime(Calendar date) {
    StringBuilder builder = new StringBuilder(16);
    appendField(builder, date.get(Calendar.YEAR), 4);
    appendField(builder, date.get(Calendar.MONTH) + 1, 2);
    appendField(builder, date.get(Calendar.DAY_OF_MONTH), 2);
    appendField(builder, date.get(Calendar.HOUR_OF_DAY), 2);
    appendField(builder, date.get(Calendar.MINUTE), 2);
    appendField(builder, date.get(Calendar.SECOND), 2);
    return builder.toString();
}