Example usage for java.util Calendar DATE

List of usage examples for java.util Calendar DATE

Introduction

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

Prototype

int DATE

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

Click Source Link

Document

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

Usage

From source file:Util.java

public static Date getSunday(Date today) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(today);/*w w  w  .  j  a  va  2  s.c o  m*/

    int dow = cal.get(Calendar.DAY_OF_WEEK);

    while (dow != Calendar.SUNDAY) {
        int date = cal.get(Calendar.DATE);

        int month = cal.get(Calendar.MONTH);

        int year = cal.get(Calendar.YEAR);

        if (date == getMonthLastDate(month, year)) {

            if (month == Calendar.DECEMBER) {
                month = Calendar.JANUARY;

                cal.set(Calendar.YEAR, year + 1);
            } else {
                month++;
            }

            cal.set(Calendar.MONTH, month);

            date = 1;
        } else {
            date++;
        }

        cal.set(Calendar.DATE, date);

        dow = cal.get(Calendar.DAY_OF_WEEK);
    }

    return cal.getTime();
}

From source file:Main.java

private static void prepareCalendarStartsMonday(Calendar calendar) {
    if (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.SUNDAY) {
            calendar.add(Calendar.DATE, -6);
        } else {//from ww  w.  j av a 2  s .c om
            calendar.add(Calendar.DATE, -dayOfWeek + 2);
        }
    }
}

From source file:Main.java

public static boolean isToday(long date) {

    Calendar compareCalendar = Calendar.getInstance();
    compareCalendar.setTime(new Date(date));

    Calendar todayCalendar = Calendar.getInstance();
    todayCalendar.setTime(new Date(System.currentTimeMillis()));

    return todayCalendar.get(Calendar.DATE) == compareCalendar.get(Calendar.DATE);
}

From source file:Main.java

public static boolean isFuture(long date) {

    Calendar compareCalendar = Calendar.getInstance();
    compareCalendar.setTime(new Date(date));

    Calendar todayCalendar = Calendar.getInstance();
    todayCalendar.setTime(new Date(System.currentTimeMillis()));

    return todayCalendar.get(Calendar.DATE) > compareCalendar.get(Calendar.DATE);
}

From source file:Main.java

public static Calendar getTomorrowAt(int hours, int minutes) {
    Calendar today = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
    cal.clear();/*from   w  ww.j  a v  a  2 s . c  om*/

    int year = today.get(Calendar.YEAR);
    int month = today.get(Calendar.MONTH);
    //represents the day of the month
    today.add(Calendar.DATE, 1);
    int day = today.get(Calendar.DATE);
    cal.set(year, month, day, hours, minutes, 0);
    return cal;
}

From source file:Main.java

public static boolean isYesterday(long date) {

    Calendar compareCalendar = Calendar.getInstance();
    compareCalendar.setTime(new Date(date));

    Calendar todayCalendar = Calendar.getInstance();
    todayCalendar.setTime(new Date(System.currentTimeMillis()));

    return todayCalendar.get(Calendar.DATE) < compareCalendar.get(Calendar.DATE);
}

From source file:Main.java

public static Calendar getTodayAt(int hours, int minutes) {
    Calendar today = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
    cal.clear();/* w  w  w . ja  v  a  2 s.c  om*/

    int year = today.get(Calendar.YEAR);
    int month = today.get(Calendar.MONTH);
    //represents the day of the month
    int day = today.get(Calendar.DATE);
    cal.set(year, month, day, hours, minutes, 0);
    return cal;
}

From source file:Main.java

public static String getNowTime(int i) {
    SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
    Date beginDate = new Date();
    Calendar date = Calendar.getInstance();
    date.setTime(beginDate);/* w ww  . j a va 2 s . c  o m*/
    date.set(Calendar.DATE, date.get(Calendar.DATE) - i);
    Date endDate = null;
    try {
        endDate = dft.parse(dft.format(date.getTime()));
        String format = dft.format(endDate);
        return format;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * Returns current date-time in format of yyyy-MM-dd hh:mm:ss
 * //from ww  w  .  j a v  a  2s  . c o m
 * @return date time in string.
 */
public static String getCurrentTime() {
    int yyyy = Calendar.getInstance().get(Calendar.YEAR);
    int MM = Calendar.getInstance().get(Calendar.MONTH) + 1;
    int dd = Calendar.getInstance().get(Calendar.DATE);
    int hh = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    int mm = Calendar.getInstance().get(Calendar.MINUTE);
    int ss = Calendar.getInstance().get(Calendar.SECOND);
    String curDate = String.format("%04d-%02d-%02d %02d:%02d:%02d", yyyy, MM, dd, hh, mm, ss);

    return curDate;

}

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 ava  2 s. 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;
}