Example usage for java.util Calendar DAY_OF_WEEK

List of usage examples for java.util Calendar DAY_OF_WEEK

Introduction

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

Prototype

int DAY_OF_WEEK

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

Click Source Link

Document

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

Usage

From source file:Main.java

public static long getPeriodStart(int periodType, long date) {
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(date);/*  w  w w  .ja v  a  2  s  . c  o  m*/

    switch (periodType) {
    case TYPE_DAY: {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_WEEK: {
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        final int currentDayOfWeek = (cal.get(Calendar.DAY_OF_WEEK) + 7 - cal.getFirstDayOfWeek()) % 7;
        cal.add(Calendar.DAY_OF_YEAR, -currentDayOfWeek);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_MONTH: {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }

    case TYPE_YEAR: {
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        break;
    }
    }
    return cal.getTimeInMillis();
}

From source file:Main.java

/**
 * Get the weekday of the calendar/* ww w  . ja  v a  2  s . co  m*/
 * @param calendar
 * @return Weekday
 */
public static String getWeekDay(Calendar calendar) {
    return getDay(calendar.get(Calendar.DAY_OF_WEEK));
}

From source file:Main.java

/**
 * @param period : today, this_week, this_month, this_year, none
 * @return a date filter string for making query. e.g. date >= 2013-1-1 for this_year
 * return null if period is none.//from  ww  w .j a  v  a2s  .c o  m
 */
@SuppressWarnings("deprecation")
public static String getDateFilterString(String period) {
    if (period == null)
        return null;
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1; // 1 - 12 
    int date = calendar.get(Calendar.DATE); // 1 - 31

    String dateFilterString = null;

    if (period.equalsIgnoreCase("today")) {
        dateFilterString = "date >= " + year + "-" + month + "-" + date;
    } else if (period.contains("week")) {
        int offset = calendar.get(Calendar.DAY_OF_WEEK) - calendar.getFirstDayOfWeek();
        long timeInMillis = calendar.getTimeInMillis() - offset * 24 * 60 * 60 * 1000;
        Date firstDay = new Date(timeInMillis);
        dateFilterString = "date >= " + (firstDay.getYear() + 1900) + "-" + firstDay.getMonth() + "-"
                + firstDay.getDate();
    } else if (period.contains("month")) {
        dateFilterString = "date >= " + year + "-" + month + "-1";
    } else if (period.contains("year")) {
        dateFilterString = "date >= " + year + "-1-1";
    }

    return dateFilterString;
}

From source file:Util.java

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

    cal.setTime(today);//from w w  w. j  a  va2 s .c  o m

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

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

        if (date == 1) {
            int month = cal.get(Calendar.MONTH);

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

                cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - 1);
            } else {
                month--;
            }

            cal.set(Calendar.MONTH, month);

            date = getMonthLastDate(month, cal.get(Calendar.YEAR));
        } else {
            date--;
        }

        cal.set(Calendar.DATE, date);

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

    return cal.getTime();
}

From source file:com.castis.xylophone.adsmadapter.common.util.CiDateUtil.java

public static int countDay(Calendar start, Calendar end, InventoryBoxDayCode dayCode) {

    int count = 0;

    while (start.compareTo(end) <= 0) {

        int day = start.get(Calendar.DAY_OF_WEEK);

        if (dayCode != null) {
            if (Calendar.MONDAY <= day && Calendar.FRIDAY >= day) {
                if (dayCode.equals(InventoryBoxDayCode.WEEKDAY))
                    count++;//from www .  j  a va2s  .co m
            } else {
                if (dayCode.equals(InventoryBoxDayCode.WEEKEND))
                    count++;
            }
        } else {
            count++;
        }

        start.add(Calendar.DAY_OF_MONTH, 1);
    }

    return count;
}

From source file:Main.java

private static String getDayOfWeek(String format, int calendarField) {
    String strDate = null;/*  w  ww .j  a va2s  .c om*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        int week = c.get(Calendar.DAY_OF_WEEK);
        if (week == calendarField) {
            strDate = mSimpleDateFormat.format(c.getTime());
        } else {
            int offectDay = calendarField - week;
            if (calendarField == Calendar.SUNDAY) {
                offectDay = 7 - Math.abs(offectDay);
            }
            c.add(Calendar.DATE, offectDay);
            strDate = mSimpleDateFormat.format(c.getTime());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

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"
 *//*  ww  w .j a  v a  2s . co  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;
}

From source file:Main.java

private static void setStartOfWeekToCalendar(Calendar c) {
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    setStartOfDayToCalendar(c);//w  w  w  . ja v  a2  s.c  om
    Log.d("La Cuenta: ", "Min of Current Week: " + DateFormat.getInstance().format(c.getTime()));
}

From source file:Main.java

public static int getWeekNumber(String strDate, String inFormat) {
    Calendar calendar = new GregorianCalendar();
    DateFormat df = new SimpleDateFormat(inFormat);
    try {//from ww  w  .ja va2s.c  o  m
        calendar.setTime(df.parse(strDate));
    } catch (Exception e) {
        return -1;
    }
    int intTemp = calendar.get(Calendar.DAY_OF_WEEK) - 1;
    return intTemp;
}

From source file:Main.java

private static void setEndOfWeekToCalendar(Calendar c) {
    // Get First Day of next week so it will be considered until 0 hrs of
    // next week//from w  w  w.j  av  a 2s .c  o  m
    c.set(Calendar.DAY_OF_WEEK, c.getMaximum(Calendar.DAY_OF_WEEK));
    setEndOfDayToCalendar(c);
    Log.d("La Cuenta: ", "Max of Current Week: " + DateFormat.getInstance().format(c.getTime()));
}