Example usage for java.util Calendar setFirstDayOfWeek

List of usage examples for java.util Calendar setFirstDayOfWeek

Introduction

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

Prototype

public void setFirstDayOfWeek(int value) 

Source Link

Document

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

Usage

From source file:com.huateng.ebank.framework.util.DateUtil.java

/**
* ?//from   ww  w. ja v  a2  s  .c om
* 
*/
public static List getEndWeekDate(Date startDate, Date endDate, SimpleDateFormat sdf) {

    Calendar cal = Calendar.getInstance();
    int days = getDaysBetween(startDate, endDate);
    List list = new ArrayList<String>();
    for (int i = 1; i < days; i++) {
        startDate = getNextDay(startDate);
        cal.setTime(startDate);
        // ?????
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// ?
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 
        int day = cal.get(Calendar.DAY_OF_WEEK);// ?
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// ???
        String imptimeBegin = sdf.format(cal.getTime());
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        list.add(imptimeEnd);
    }
    // ??
    Set set = new HashSet();
    List newList = new ArrayList();
    for (Iterator iter = list.iterator(); iter.hasNext();) {
        Object element = iter.next();

        if (set.add(element))
            newList.add(element);
    }
    list.clear();
    list.addAll(newList);

    return newList;
}

From source file:org.pentaho.di.core.row.ValueDataUtil.java

public static Object weekOfYearISO8601(ValueMetaInterface metaA, Object dataA) throws KettleValueException {
    if (dataA == null) {
        return null;
    }/*from   ww w  .ja  va 2  s .  c  om*/

    Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
    calendar.setMinimalDaysInFirstWeek(4);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setTime(metaA.getDate(dataA));
    return new Long(calendar.get(Calendar.WEEK_OF_YEAR));

}

From source file:org.pentaho.di.core.row.ValueDataUtil.java

public static Object yearOfDateISO8601(ValueMetaInterface metaA, Object dataA) throws KettleValueException {
    if (dataA == null) {
        return null;
    }//  w  w  w.  j  a  va 2 s  .c  om

    Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
    calendar.setMinimalDaysInFirstWeek(4);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setTime(metaA.getDate(dataA));

    int week = calendar.get(Calendar.WEEK_OF_YEAR);
    int month = calendar.get(Calendar.MONTH);
    int year = calendar.get(Calendar.YEAR);

    // fix up for the year taking into account ISO8601 weeks
    if (week >= 52 && month == 0) {
        year--;
    }
    if (week <= 2 && month == 11) {
        year++;
    }

    return new Long(year);
}

From source file:com.baidu.rigel.biplatform.tesseract.meta.impl.TimeDimensionMemberServiceImpl.java

/**
 * ??/* w  ww  .  ja  v  a 2s .  c o m*/
 * 
 * @param date
 * @return
 */
private Date getFirstDayOfWeek(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    return cal.getTime();
}

From source file:com.samsistemas.calendarview.widget.CalendarView.java

public void setDateAsSelected(Date currentDate) {
    final Calendar currentCalendar = CalendarUtility.getTodayCalendar(mContext, mFirstDayOfWeek);
    currentCalendar.setFirstDayOfWeek(mFirstDayOfWeek);
    currentCalendar.setTime(currentDate);

    // Clear previous marks
    clearDayOfTheMonthStyle(mLastSelectedDay);

    // Store current values as last values
    setLastSelectedDay(currentDate);/*from w  ww . ja v  a  2 s.  c  o  m*/

    // Mark current day as selected
    DayView view = findViewByCalendar(currentCalendar);
    view.setBackgroundColor(mSelectedDayBackground);
    view.setTextColor(mSelectedDayTextColor);
}

From source file:com.samsistemas.calendarview.widget.CalendarView.java

private void clearDayOfTheMonthStyle(Date currentDate) {
    if (currentDate != null) {
        final Calendar calendar = CalendarUtility.getTodayCalendar(mContext, mFirstDayOfWeek);
        calendar.setFirstDayOfWeek(mFirstDayOfWeek);
        calendar.setTime(currentDate);/*from   w  w w  .jav  a  2s .c  o m*/

        final DayView dayView = findViewByCalendar(calendar);
        dayView.setBackgroundColor(mCalendarBackgroundColor);
        mIsCommonDay = true;
        if (totalDayOfWeekend().length != 0) {
            for (int weekend : totalDayOfWeekend()) {
                if (calendar.get(Calendar.DAY_OF_WEEK) == weekend) {
                    dayView.setTextColor(mWeekendColor);
                    mIsCommonDay = false;
                }
            }
        }

        if (mIsCommonDay) {
            dayView.setTextColor(mDayOfWeekTextColor);
        }
    }
}

From source file:com.samsistemas.calendarview.widget.CalendarView.java

/**
 * This method prepare and populate the days in the CalendarView
 *///  w w  w. j  av a 2s.  c  o  m
private void setDaysInCalendar() {
    Calendar calendar = Calendar.getInstance(getLocale());
    calendar.setTime(mCalendar.getTime());
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.setFirstDayOfWeek(mFirstDayOfWeek);
    int firstDayOfMonth = calendar.get(Calendar.DAY_OF_WEEK);

    // Calculate dayOfMonthIndex
    int dayOfMonthIndex = CalendarUtility.getWeekIndex(firstDayOfMonth, calendar);
    int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    final Calendar startCalendar = (Calendar) calendar.clone();
    //Add required number of days
    startCalendar.add(Calendar.DATE, -(dayOfMonthIndex - 1));
    int monthEndIndex = 42 - (actualMaximum + dayOfMonthIndex - 1);

    DayView dayView;
    ViewGroup dayOfMonthContainer;
    for (int i = 1; i < 43; i++) {
        dayOfMonthContainer = (ViewGroup) mView
                .findViewWithTag(mContext.getString(R.string.day_of_month_container) + i);
        dayView = (DayView) mView.findViewWithTag(mContext.getString(R.string.day_of_month_text) + i);
        if (dayView == null)
            continue;

        //Apply the default styles
        dayOfMonthContainer.setOnClickListener(null);
        dayView.bind(startCalendar.getTime(), getDecoratorsList());
        dayView.setVisibility(View.VISIBLE);

        if (null != getTypeface()) {
            dayView.setTypeface(getTypeface());
        }

        if (CalendarUtility.isSameMonth(calendar, startCalendar)) {
            dayOfMonthContainer.setOnClickListener(onDayOfMonthClickListener);
            dayOfMonthContainer.setOnLongClickListener(onDayOfMonthLongClickListener);
            dayView.setBackgroundColor(mCalendarBackgroundColor);
            mIsCommonDay = true;
            if (totalDayOfWeekend().length != 0) {
                for (int weekend : totalDayOfWeekend()) {
                    if (startCalendar.get(Calendar.DAY_OF_WEEK) == weekend) {
                        dayView.setTextColor(mWeekendColor);
                        mIsCommonDay = false;
                    }
                }
            }

            if (mIsCommonDay) {
                dayView.setTextColor(mDayOfWeekTextColor);
            }
        } else {
            dayView.setBackgroundColor(mDisabledDayBackgroundColor);
            dayView.setTextColor(mDisabledDayTextColor);

            if (!isOverflowDateVisible())
                dayView.setVisibility(View.GONE);
            else if (i >= 36 && ((float) monthEndIndex / 7.0f) >= 1) {
                dayView.setVisibility(View.GONE);
            }
        }
        dayView.decorate();

        //Set the current day color
        if (mCalendar.get(Calendar.MONTH) == startCalendar.get(Calendar.MONTH))
            setCurrentDay(mCalendar.getTime());

        startCalendar.add(Calendar.DATE, 1);
        dayOfMonthIndex++;
    }

    // If the last week row has no visible days, hide it or show it in case
    ViewGroup weekRow = (ViewGroup) mView.findViewWithTag("weekRow6");
    dayView = (DayView) mView.findViewWithTag("dayOfMonthText36");
    if (dayView.getVisibility() != VISIBLE) {
        weekRow.setVisibility(GONE);
    } else {
        weekRow.setVisibility(VISIBLE);
    }
}

From source file:com.baidu.rigel.biplatform.tesseract.meta.impl.TimeDimensionMemberServiceImpl.java

/**
 * ?//from   ww w. ja v  a  2 s  .com
 * 
 * @param level
 * @param parentMember
 * @return
 */
private List<MiniCubeMember> genMembersFromOtherToWeek(Level level, Member parentMember) {
    List<MiniCubeMember> members = Lists.newArrayList();
    String parentName = parentMember.getName();
    String year = parentName.substring(0, 4);
    String month = parentName.substring(4, 6);
    int quarterIndex = Integer.valueOf(month) - 1; // 
    Calendar cal = Calendar.getInstance();
    Date now = cal.getTime(); // ?
    Date nowWeek = this.getFirstDayOfWeek(now); // ?

    cal.set(Calendar.YEAR, Integer.valueOf(year));
    cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
    cal.set(Calendar.DATE, 1);
    cal.setFirstDayOfWeek(Calendar.MONDAY);

    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
    int count = 1; // 
    Date firstDay = cal.getTime();
    Date firstWeek = this.getFirstDayOfWeek(firstDay); // ?
    Date lastDay;
    Date lastWeek;
    int daysOfMonth;
    switch (parentMember.getLevel().getType()) {
    case TIME_YEARS:
        break;
    case TIME_QUARTERS:
        cal.add(Calendar.MONTH, 2); // ??
        daysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // ?
        cal.add(Calendar.DATE, daysOfMonth);
        lastDay = cal.getTime(); // 
        lastWeek = this.getFirstDayOfWeek(lastDay); // 
        // ???
        if (nowWeek.before(lastWeek) || (nowWeek.compareTo(lastWeek) == 0)) {
            lastWeek = nowWeek;
        }
        cal.setTime(firstWeek); // ?
        while (cal.getTime().before(lastWeek) || (cal.getTime().compareTo(lastWeek) == 0)) {
            String day = sf.format(cal.getTime());
            String caption = "" + (quarterIndex + 1) + "" + count + "";
            caption = caption + day;
            MiniCubeMember dayMember = new MiniCubeMember(day);
            dayMember.setCaption(caption);
            dayMember.setLevel(level);
            dayMember.setParent(parentMember);
            dayMember.setName(day);
            dayMember.setVisible(true);
            for (int i = 0; i <= 6; i++) {
                dayMember.getQueryNodes().add(sf.format(cal.getTime()));
                cal.add(Calendar.DATE, 1);
            }
            members.add(dayMember);
            // cal.add(Calendar.DATE, 1);
            count++;
        }
        break;
    case TIME_MONTHS:
        daysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 
        cal.add(Calendar.DATE, daysOfMonth);
        lastDay = cal.getTime(); // 
        lastWeek = this.getFirstDayOfWeek(lastDay); // 
        // ???
        if (nowWeek.before(lastWeek) || (nowWeek.compareTo(lastWeek) == 0)) {
            lastWeek = nowWeek;
        }
        cal.setTime(firstWeek); // ?
        while (cal.getTime().before(lastWeek) || (cal.getTime().compareTo(lastWeek) == 0)) {
            String day = sf.format(cal.getTime());
            String caption = month + "" + count + "";
            MiniCubeMember dayMember = new MiniCubeMember(day);
            dayMember.setCaption(caption);
            dayMember.setLevel(level);
            dayMember.setParent(parentMember);
            dayMember.setName(day);
            dayMember.setVisible(true);
            for (int i = 0; i <= 6; i++) {
                dayMember.getQueryNodes().add(sf.format(cal.getTime()));
                cal.add(Calendar.DATE, 1);
            }
            members.add(dayMember);
            // cal.add(Calendar.DATE, 1);
            count++;
        }
        break;
    case TIME_WEEKS:
        break;
    case TIME_DAYS:
        break;
    default:
    }
    return members;
}

From source file:com.markuspage.android.atimetracker.Tasks.java

private void switchView(int which) {
    Calendar tw = Calendar.getInstance();
    int startDay = preferences.getInt(START_DAY, 0) + 1;
    tw.setFirstDayOfWeek(startDay);
    String ttl = getString(R.string.title, getResources().getStringArray(R.array.views)[which]);
    switch (which) {
    case 0: // today
        adapter.loadTasks(tw);/*from w  w  w  .  ja  v  a  2 s . co  m*/
        break;
    case 1: // this week
        adapter.loadTasks(weekStart(tw, startDay), weekEnd(tw, startDay));
        break;
    case 2: // yesterday
        tw.add(Calendar.DAY_OF_MONTH, -1);
        adapter.loadTasks(tw);
        break;
    case 3: // last week
        tw.add(Calendar.WEEK_OF_YEAR, -1);
        adapter.loadTasks(weekStart(tw, startDay), weekEnd(tw, startDay));
        break;
    case 4: // all
        adapter.loadTasks();
        break;
    case 5: // select range
        Calendar start = Calendar.getInstance();
        start.setTimeInMillis(preferences.getLong(START_DATE, 0));
        System.err.println("START = " + start.getTime());
        Calendar end = Calendar.getInstance();
        end.setTimeInMillis(preferences.getLong(END_DATE, 0));
        System.err.println("END = " + end.getTime());
        adapter.loadTasks(start, end);
        DateFormat f = DateFormat.getDateInstance(DateFormat.SHORT);
        ttl = getString(R.string.title, f.format(start.getTime()) + " - " + f.format(end.getTime()));
        break;
    default: // Unknown
        break;
    }
    baseTitle = ttl;
    setTitle();
    getListView().invalidate();
}

From source file:com.saulcintero.moveon.fragments.History.java

private void getWeek() {
    Calendar c1 = Calendar.getInstance();

    // first day of week
    c1.setFirstDayOfWeek(Calendar.MONDAY);
    c1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

    int y = c1.get(Calendar.YEAR);
    int m = c1.get(Calendar.MONTH) + 1;
    int d = c1.get(Calendar.DAY_OF_MONTH);

    mYear1 = String.valueOf(y);//  ww  w  . java  2s  .  co m
    mMonth1 = String.valueOf(m);
    if (m < 10)
        mMonth1 = "0" + mMonth1;
    mDay1 = String.valueOf(d);
    if (d < 10)
        mDay1 = "0" + mDay1;

    // last day of week
    c1.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

    int y2 = c1.get(Calendar.YEAR);
    int m2 = c1.get(Calendar.MONTH) + 1;
    int d2 = c1.get(Calendar.DAY_OF_MONTH);

    mYear2 = String.valueOf(y2);
    mMonth2 = String.valueOf(m2);
    if (m2 < 10)
        mMonth2 = "0" + mMonth2;
    mDay2 = String.valueOf(d2);
    if (d2 < 10)
        mDay2 = "0" + mDay2;
}