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 Calendar getRandomApodDate() {
    Calendar newCalendar = Calendar.getInstance();

    Calendar today = Calendar.getInstance();
    int currentYear = today.get(Calendar.YEAR);
    int currentMonth = today.get(Calendar.MONTH);
    int currentDay = today.get(Calendar.DATE);

    int firstApodYear = 1995;
    int firstApodMonth = 5;
    int firstApodDay = 20;

    Random rnd = new Random();
    int newYear = rnd.nextInt(currentYear - firstApodYear + 1) + firstApodYear;

    int minMonth = 0;
    int maxMonth = 11;
    if (newYear == firstApodYear)
        minMonth = firstApodMonth;//from w w  w.j  a  v  a 2s  .c o m
    if (newYear == currentYear)
        maxMonth = currentMonth;
    int newMonth = rnd.nextInt(maxMonth - minMonth + 1) + minMonth;

    newCalendar.set(newYear, newMonth, 1);

    int minDay = 1;
    int maxDay = newCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    if (newYear == firstApodYear && newMonth == firstApodMonth)
        minDay = firstApodDay;
    if (newYear == currentYear && newMonth == currentMonth)
        maxDay = currentDay;
    int newDay = rnd.nextInt(maxDay - minDay + 1) + minDay;

    newCalendar.set(newYear, newMonth, newDay);
    return newCalendar;
}

From source file:Main.java

public static int getDay(String strDate, String format) {
    int day = 0;/*from  w w w  .  j  av a2  s .  c  om*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(mSimpleDateFormat.parse(strDate));
        day = c.get(Calendar.DAY_OF_MONTH);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return day;
}

From source file:Main.java

/**
 * Get the age of the user. Takes in their birthday and calculates it according to today's date
 * @param birthday Date Object/* w w w.j a v a2 s  .  co  m*/
 * @return Returns an int of their age (IE 20, 55, 18). If the date is in the future, it will
 *         return -1 instead.
 */
public static int getAge(Date birthday) {

    Calendar now = Calendar.getInstance();
    Calendar dob = Calendar.getInstance();
    dob.setTime(birthday);

    //First check for in the future:
    if (dob.after(now)) {
        return -1;
    }

    int year1 = now.get(Calendar.YEAR);
    int year2 = dob.get(Calendar.YEAR);

    int age = year1 - year2;

    int month1 = now.get(Calendar.MONTH);
    int month2 = dob.get(Calendar.MONTH);

    if (month2 > month1) {
        age--;

    } else if (month1 == month2) {
        int day1 = now.get(Calendar.DAY_OF_MONTH);
        int day2 = dob.get(Calendar.DAY_OF_MONTH);
        if (day2 > day1) {
            age--;
        }
    }

    return age;
}

From source file:Main.java

private static void setStartOfMonthToCalendar(Calendar c) {
    c.set(Calendar.DAY_OF_MONTH, c.getMinimum(Calendar.DAY_OF_MONTH));
    setStartOfDayToCalendar(c);/* ww  w .  j  a v  a2  s  .  c o  m*/
    Log.d("La Cuenta: ", "Min of Current Month: " + DateFormat.getInstance().format(c.getTime()));
}

From source file:Main.java

public static Date getDateDayBefore(Date originalDate, int days) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(originalDate);/*from   w  w w  .  j a v  a 2s  .co  m*/
    cal.add(Calendar.DAY_OF_MONTH, days * -1);
    return cal.getTime();
}

From source file:Main.java

public static Date firstTimeOfMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date.getTime());

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    Date dateBegin = new Date();
    dateBegin.setTime(calendar.getTimeInMillis());
    return dateBegin;
}

From source file:Main.java

/** 
 * Adds to the given <code>JComponent</code> two date spinners 
 * labeled "To" and "From". Used to specify a specific date range. The 
 * default range is -10 years from today's date through today's date. 
 * @param c - <code>JComponent</code> to add the date spinners to
 *//*from www  .j a  v a 2s  .  c o m*/
public static void addDateRangePanel(JComponent c) {
    Calendar calendar = Calendar.getInstance();
    JSpinner dateSpinner;

    //Set up dates
    Date initDate = calendar.getTime();
    Date latestDate = calendar.getTime();
    calendar.add(Calendar.YEAR, -10);
    Date earliestDate = calendar.getTime();

    //Date Spinners
    SpinnerModel fromModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.DAY_OF_MONTH);
    dateSpinner = addLabeledSpinner(c, "From: ", fromModel, false);
    dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "MM/dd/yyyy"));

    SpinnerModel toModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.DAY_OF_MONTH);
    dateSpinner = addLabeledSpinner(c, "To: ", toModel, true);
    dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "MM/dd/yyyy"));
}

From source file:Main.java

/***********************************************************************************************
 * ...//from ww  w.j  av  a 2s.co  m
 * ********************************************************************************************/

public static String dateToFormattedString(Date date) {

    String[] suffixes =
            //  0     1     2     3     4     5     6     7     8     9
            { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
                    //10   11    12    13    14    15    16    17    18    19
                    "th", "th", "th", "th", "th", "th", "th", "th", "th", "th",
                    //20   21    22    23    24    25    26    27    28    29
                    "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
                    //30   31
                    "th", "st" };

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    int day = calendar.get(Calendar.DAY_OF_MONTH);
    String dayStr = day + suffixes[day];

    SimpleDateFormat simpleDateFormatDay = new SimpleDateFormat("EEEE");
    String formattedDateDay = simpleDateFormatDay.format(date);

    SimpleDateFormat simpleDateFormatRest = new SimpleDateFormat("LLLL");// yyyy");
    String formattedDateRest = simpleDateFormatRest.format(date);

    return formattedDateDay + ", " + dayStr + " " + formattedDateRest;
}

From source file:Main.java

public static int compare(Calendar date1, Calendar date2) {
    int i = date1.get(Calendar.YEAR);
    int j = date2.get(Calendar.YEAR);

    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.MONTH);
    j = date2.get(Calendar.MONTH);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.DAY_OF_MONTH);
    j = date2.get(Calendar.DAY_OF_MONTH);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.HOUR_OF_DAY);
    j = date2.get(Calendar.HOUR_OF_DAY);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.MINUTE);
    j = date2.get(Calendar.MINUTE);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.SECOND);
    j = date2.get(Calendar.SECOND);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    return 0;/*w  w w .j a  va  2s  . c om*/
}

From source file:Main.java

/**
 * Add date time with one day.//w  w w .j  av a 2s . c o m
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithOneDay(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 ? -1 : 1);
    listing.add(calendar.getTimeInMillis());
}