Example usage for java.util Calendar getFirstDayOfWeek

List of usage examples for java.util Calendar getFirstDayOfWeek

Introduction

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

Prototype

public int getFirstDayOfWeek() 

Source Link

Document

Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.

Usage

From source file:com.px100systems.util.SpringELCtx.java

/**
 * Period start//from  w  w w.  j  a v  a  2s .  co m
 * @param date time
 * @param arg d/w/M/y
 * @return adjusted time
 */
public static Date periodStart(Date date, String arg) {
    if (date == null)
        return null;

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    if (arg.endsWith("d"))
        return cal.getTime();
    else if (arg.endsWith("M")) {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    } else if (arg.endsWith("y")) {
        cal.set(Calendar.DAY_OF_YEAR, 1);
        return cal.getTime();
    } else if (arg.endsWith("w")) {
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        return cal.getTime();
    }

    return date;
}

From source file:cn.mljia.common.notify.utils.DateUtils.java

/**
 * ?//from w ww.  ja v  a  2  s  .  c  o m
 * 
 * @param date
 * @return
 */
public static Date getWeekStart(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.get(Calendar.WEEK_OF_YEAR);
    int firstDay = calendar.getFirstDayOfWeek();
    calendar.set(Calendar.DAY_OF_WEEK, firstDay);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTime();
}

From source file:cn.mljia.common.notify.utils.DateUtils.java

/**
 * ?/*from w  w w .  ja  v  a  2  s .c o m*/
 * 
 * @param date
 * @return
 */
public static Date getWeekEnd(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.get(Calendar.WEEK_OF_YEAR);
    int firstDay = calendar.getFirstDayOfWeek();
    calendar.set(Calendar.DAY_OF_WEEK, 8 - firstDay);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTime();
}

From source file:com.rogchen.common.xml.UtilDateTime.java

/**
 * Returns a List of day name Strings - suitable for calendar headings.
 *
 * @param locale/*from  w w  w . j a  va2 s .  com*/
 * @return List of day name Strings
 */
public static List<String> getDayNames(Locale locale) {
    Calendar tempCal = Calendar.getInstance(locale);
    tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek());
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE", locale);
    List<String> resultList = new ArrayList<String>();
    for (int i = 0; i < 7; i++) {
        resultList.add(dateFormat.format(tempCal.getTime()));
        tempCal.roll(Calendar.DAY_OF_WEEK, 1);
    }
    return resultList;
}

From source file:com.alkacon.opencms.calendar.CmsCalendarMonthBean.java

/**
 * Returns the days of a month to display in a matrix, depending on the start day of the week.<p>
 * /* ww  w.  ja va  2  s.  c  o m*/
 * The month matrix starts with index "1" and uses 7 columns per row to display one week in a row.
 * The value returns null if no date should be shown at the current index position.<p>
 * 
 * @param year the year of the month to display
 * @param month the month to display
 * @param calendarLocale the Locale for the calendar to determine the start day of the week
 * @return the days of a month to display in a matrix, depending on the start day of the week
 */
public Map getMonthDaysMatrix(int year, int month, Locale calendarLocale) {

    Map monthDays = new TreeMap();
    Calendar startDay = new GregorianCalendar(year, month, 1);

    Calendar runDay = startDay;
    int index = 1;

    // calculate the start day of the week
    Calendar calendar = new GregorianCalendar(calendarLocale);
    int weekStart = calendar.getFirstDayOfWeek();

    // create empty indexes before the first day of the month
    while (runDay.get(Calendar.DAY_OF_WEEK) != weekStart) {
        monthDays.put(new Integer(index), null);
        index++;

        if (weekStart == Calendar.SATURDAY) {
            weekStart = Calendar.SUNDAY;
        } else {
            weekStart++;
        }
    }

    // create the indexes for the month dates
    while (true) {
        monthDays.put(new Integer(index), runDay.clone());
        // increase day to next day
        runDay.roll(Calendar.DAY_OF_MONTH, true);
        index++;
        if (runDay.get(Calendar.DAY_OF_MONTH) == 1) {
            // runDay has switched to the next month, stop loop
            break;
        }
    }

    // create empty indexes after the last day of the month
    int rest = (index - 1) % 7;
    if (rest > 0) {
        rest = 7 - rest;
    }
    for (int i = 0; i < rest; i++) {
        monthDays.put(new Integer(index), null);
        index++;
    }

    return monthDays;
}

From source file:DateUtil.java

/**
 * Sets the date/time of the Calendar object to the beginning of the Period by
 * setting all fields smaller than the specified period to the minimum value.
 * // w  w  w. j  a  va 2s.  c  o  m
 * @param c
 *          The calendar to set.
 * @param p
 *          The DateUtil.Period to set.
 */
public static void setToPeriodStart(Calendar c, Period p) {
    switch (p) {
    case YEAR:
        c.set(Calendar.MONTH, 0);
    case MONTH:
        c.set(Calendar.DAY_OF_MONTH, 1);
    case DAY:
        c.set(Calendar.HOUR_OF_DAY, 0);
    case HOUR:
        c.set(Calendar.MINUTE, 0);
    case MINUTE:
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        break;
    case WEEK:
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        break;
    case AMPM:
        if (c.get(Calendar.AM_PM) == Calendar.AM)
            c.set(Calendar.HOUR_OF_DAY, 0);
        else
            c.set(Calendar.HOUR_OF_DAY, 12);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        break;
    case QUARTER:
        int month = c.get(Calendar.MONTH);
        if (month >= 9)
            c.set(Calendar.MONTH, 9);
        else if (month >= 9)
            c.set(Calendar.MONTH, 6);
        else if (month >= 9)
            c.set(Calendar.MONTH, 3);
        else
            c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        break;
    }
    return;
}

From source file:com.aurel.track.fieldType.runtime.matchers.run.DateMatcherRT.java

/**
 * Reinitialize dayBegin and dayEnd/*from  w w  w  .ja  v  a  2s .com*/
 */
@Override
public void setMatchValue(Object matcherValue) {
    super.setMatchValue(matcherValue);
    if (relation == MatchRelations.LATER_AS_LASTLOGIN && matcherContext != null) {
        matcherValue = matcherContext.getLastLoggedDate();
    }
    Calendar calendaMatcherValue = new GregorianCalendar(matcherContext.getLocale());
    calendaMatcherValue.setTime(new Date());
    calendaMatcherValue = CalendarUtil.clearTime(calendaMatcherValue);
    int firstDayOfWeek = calendaMatcherValue.getFirstDayOfWeek();
    switch (relation) {
    case MatchRelations.THIS_WEEK:
        calendaMatcherValue.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        dayBegin = calendaMatcherValue.getTime();
        calendaMatcherValue.add(Calendar.WEEK_OF_YEAR, 1);
        calendaMatcherValue.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        dayEnd = calendaMatcherValue.getTime();
        return;
    case MatchRelations.LAST_WEEK:
        calendaMatcherValue.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        dayEnd = calendaMatcherValue.getTime();
        calendaMatcherValue.add(Calendar.WEEK_OF_YEAR, -1);
        calendaMatcherValue.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        dayBegin = calendaMatcherValue.getTime();
        return;
    case MatchRelations.THIS_MONTH:
        calendaMatcherValue.set(Calendar.DAY_OF_MONTH, 1);
        dayBegin = calendaMatcherValue.getTime();
        calendaMatcherValue.add(Calendar.MONTH, 1);
        calendaMatcherValue.set(Calendar.DAY_OF_MONTH, 1);
        dayEnd = calendaMatcherValue.getTime();
        return;
    case MatchRelations.LAST_MONTH:
        calendaMatcherValue.set(Calendar.DAY_OF_MONTH, 1);
        dayEnd = calendaMatcherValue.getTime();
        calendaMatcherValue.add(Calendar.MONTH, -1);
        calendaMatcherValue.set(Calendar.DAY_OF_MONTH, 1);
        dayBegin = calendaMatcherValue.getTime();
        return;
    }

    if (matcherValue == null) {
        return;
    }
    //reinitialize dayBegin and dayEnd
    Date matcherValueDate = null;
    Integer matcherValueInteger = null;
    switch (relation) {
    case MatchRelations.MORE_THAN_DAYS_AGO:
    case MatchRelations.MORE_THAN_EQUAL_DAYS_AGO:
    case MatchRelations.LESS_THAN_DAYS_AGO:
    case MatchRelations.LESS_THAN_EQUAL_DAYS_AGO:
        try {
            matcherValueInteger = (Integer) matchValue;
        } catch (Exception e) {
            LOGGER.warn("Converting the matcher value " + matchValue + " of type "
                    + matchValue.getClass().getName() + " to Integer failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        calendaMatcherValue.setTime(new Date());
        if (matcherValueInteger != null) {
            int matcherValueInt = matcherValueInteger.intValue();
            if (relation == MatchRelations.MORE_THAN_EQUAL_DAYS_AGO) {
                matcherValueInt--;
            }
            if (relation == MatchRelations.LESS_THAN_EQUAL_DAYS_AGO) {
                matcherValueInt++;
            }
            calendaMatcherValue.add(Calendar.DATE, -matcherValueInt);
        }
        break;
    case MatchRelations.IN_MORE_THAN_DAYS:
    case MatchRelations.IN_MORE_THAN_EQUAL_DAYS:
    case MatchRelations.IN_LESS_THAN_DAYS:
    case MatchRelations.IN_LESS_THAN_EQUAL_DAYS:
        try {
            matcherValueInteger = (Integer) matchValue;
        } catch (Exception e) {
            LOGGER.warn("Converting the matcher value " + matchValue + " of type "
                    + matchValue.getClass().getName() + " to Integer failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        calendaMatcherValue.setTime(new Date());
        if (matcherValueInteger != null) {
            int matcherValueInt = matcherValueInteger.intValue();
            if (relation == MatchRelations.IN_MORE_THAN_EQUAL_DAYS) {
                matcherValueInt--;
            }
            if (relation == MatchRelations.IN_LESS_THAN_EQUAL_DAYS) {
                matcherValueInt++;
            }
            calendaMatcherValue.add(Calendar.DATE, matcherValueInt);
        }
        break;
    case MatchRelations.EQUAL_DATE:
    case MatchRelations.NOT_EQUAL_DATE:
    case MatchRelations.GREATHER_THAN_DATE:
    case MatchRelations.GREATHER_THAN_EQUAL_DATE:
    case MatchRelations.LESS_THAN_DATE:
    case MatchRelations.LESS_THAN_EQUAL_DATE:
        try {
            matcherValueDate = (Date) matchValue;
        } catch (Exception e) {
            LOGGER.warn("Converting the matcher value " + matchValue + " of type "
                    + matchValue.getClass().getName() + " to Date failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        if (matcherValueDate != null) {
            calendaMatcherValue.setTime(matcherValueDate);
            if (relation == MatchRelations.GREATHER_THAN_EQUAL_DATE) {
                calendaMatcherValue.add(Calendar.DATE, -1);
            } else {
                if (relation == MatchRelations.LESS_THAN_EQUAL_DATE) {
                    calendaMatcherValue.add(Calendar.DATE, 1);
                }
            }
        }
        break;
    case MatchRelations.LATER_AS_LASTLOGIN:
        if (matcherContext != null) {
            dayBegin = matcherContext.getLastLoggedDate();
            dayEnd = matcherContext.getLastLoggedDate();
        }
        //return because later the dayBegin and dayEnd will be assigned again
        return;
    }

    calendaMatcherValue = CalendarUtil.clearTime(calendaMatcherValue);
    dayBegin = calendaMatcherValue.getTime();
    calendaMatcherValue.add(Calendar.DATE, 1);
    dayEnd = calendaMatcherValue.getTime();
}

From source file:com.xandy.calendar.month.SimpleDayPickerFragment.java

/**
 * Updates the user preference fields. Override this to use a different
 * preference space./*from   ww w.j a v  a  2 s  .  c  o m*/
 */
protected void doResumeUpdates() {
    // Get default week start based on locale, subtracting one for use with android Time.
    Calendar cal = Calendar.getInstance(Locale.getDefault());
    mFirstDayOfWeek = cal.getFirstDayOfWeek() - 1;

    mShowWeekNumber = false;

    updateHeader();
    goTo(mSelectedDay.toMillis(true), false, false, false);
    mAdapter.setSelectedDay(mSelectedDay);
    mTodayUpdater.run();
}

From source file:com.todoroo.astrid.repeats.RepeatControlSet.java

@Nullable
@Override/*w  w w .j av a  2 s  .c o m*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    if (savedInstanceState != null) {
        recurrence = savedInstanceState.getString(EXTRA_RECURRENCE);
        repeatUntilValue = savedInstanceState.getLong(EXTRA_REPEAT_UNTIL);
        repeatAfterCompletion = savedInstanceState.getBoolean(EXTRA_REPEAT_AFTER_COMPLETION);
    }

    dialogView = inflater.inflate(R.layout.control_set_repeat, null);
    value = (Button) dialogView.findViewById(R.id.repeatValue);
    Spinner interval = (Spinner) dialogView.findViewById(R.id.repeatInterval);
    ArrayAdapter<String> intervalAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item,
            getResources().getStringArray(R.array.repeat_interval));
    intervalAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    interval.setAdapter(intervalAdapter);
    Spinner type = (Spinner) dialogView.findViewById(R.id.repeatType);
    ArrayAdapter<String> typeAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item,
            getResources().getStringArray(R.array.repeat_type));
    typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    type.setAdapter(typeAdapter);
    type.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            repeatAfterCompletion = position == TYPE_COMPLETION_DATE;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    daysOfWeekContainer = (LinearLayout) dialogView.findViewById(R.id.repeatDayOfWeekContainer);
    repeatUntil = (Spinner) dialogView.findViewById(R.id.repeat_until);
    repeatUntilAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, repeatUntilOptions);
    repeatUntilAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    repeatUntil.setAdapter(repeatUntilAdapter);
    // set up days of week
    DateFormatSymbols dfs = new DateFormatSymbols();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
    CompoundButton[] daysOfWeek = new CompoundButton[7];
    for (int i = 0; i < 7; i++) {
        final int index = i;
        CheckBox checkBox = (CheckBox) daysOfWeekContainer.getChildAt(i);
        checkBox.setOnCheckedChangeListener(
                (buttonView, isChecked1) -> RepeatControlSet.this.isChecked[index] = isChecked1);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        checkBox.setText(dfs.getShortWeekdays()[dayOfWeek].substring(0, 1));
        daysOfWeek[i] = checkBox;
        weekdays[i] = Weekday.values()[dayOfWeek - 1];
        calendar.add(Calendar.DATE, 1);
    }

    // set up listeners
    value.setOnClickListener(v -> repeatValueClick());

    setRepeatValue(1);
    interval.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View view, int position, long id) {
            daysOfWeekContainer.setVisibility(position == INTERVAL_WEEKS ? View.VISIBLE : View.GONE);
            intervalValue = position;
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            //
        }
    });

    setRepeatUntilValue(repeatUntilValue);
    repeatUntil.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            if (repeatUntilOptions.size() == 2) {
                if (i == 0) {
                    setRepeatUntilValue(0);
                } else {
                    repeatUntilClick();
                }
            } else {
                if (i == 1) {
                    setRepeatUntilValue(0);
                } else if (i == 2) {
                    repeatUntilClick();
                }
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            //
        }
    });

    daysOfWeekContainer.setVisibility(View.GONE);
    type.setSelection(repeatAfterCompletion ? TYPE_COMPLETION_DATE : TYPE_DUE_DATE);
    doRepeat = !Strings.isNullOrEmpty(recurrence);
    if (doRepeat) {
        // read recurrence rule
        try {
            RRule rrule = new RRule(recurrence);

            setRepeatValue(rrule.getInterval());

            for (WeekdayNum day : rrule.getByDay()) {
                for (int i = 0; i < 7; i++) {
                    if (weekdays[i].equals(day.wday)) {
                        daysOfWeek[i].setChecked(true);
                    }
                }
            }

            switch (rrule.getFreq()) {
            case DAILY:
                intervalValue = INTERVAL_DAYS;
                break;
            case WEEKLY:
                intervalValue = INTERVAL_WEEKS;
                break;
            case MONTHLY:
                intervalValue = INTERVAL_MONTHS;
                break;
            case HOURLY:
                intervalValue = INTERVAL_HOURS;
                break;
            case MINUTELY:
                intervalValue = INTERVAL_MINUTES;
                break;
            case YEARLY:
                intervalValue = INTERVAL_YEARS;
                break;
            default:
                Timber.e(new Exception("Unhandled rrule frequency: " + recurrence), "repeat-unhandled-rule");
            }
            interval.setSelection(intervalValue);

        } catch (Exception e) {
            // invalid RRULE
            recurrence = ""; //$NON-NLS-1$
            Timber.e(e, e.getMessage());
        }
    }
    refreshDisplayView();
    return view;
}

From source file:com.android.calendar.month.SimpleDayPickerFragment.java

/**
 * Updates the user preference fields. Override this to use a different
 * preference space./*from   ww  w.  ja va2  s.c  o m*/
 */
protected void doResumeUpdates() {
    // Get default week start based on locale, subtracting one for use with
    // android Time.
    Calendar cal = Calendar.getInstance(Locale.getDefault());
    mFirstDayOfWeek = cal.getFirstDayOfWeek() - 1;

    mShowWeekNumber = false;

    updateHeader();
    goTo(mSelectedDay.toMillis(true), false, false, false);
    mAdapter.setSelectedDay(mSelectedDay);
    mTodayUpdater.run();
}