Java Code Examples for java.util.Calendar.setFirstDayOfWeek()

The following are Jave code examples for showing how to use setFirstDayOfWeek() of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 8 votes
@Override
public String format(DateTime timestamp) {
    Calendar c = Calendar.getInstance(timestamp.getZone().toTimeZone(), LOCALE);
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setMinimalDaysInFirstWeek(4);
    c.setTimeInMillis(timestamp.getMillis());

    int week = c.get(Calendar.WEEK_OF_YEAR);
    int weekYear = c.getWeekYear();
    int year = c.get(Calendar.YEAR);
    if (weekYear < year) {
        // get weeks from last year
        c.add(Calendar.DAY_OF_MONTH, -7);
        week = c.getWeeksInWeekYear();
    }
    return zeroPadded(2, String.valueOf(week));
}
 
Example 2
Project: lorne_core   File: DateUtil.java   View Source Code Vote up 8 votes
/**
 * 一周的日期
 * @param date  时间
 * @return  结果
 */
public static List<Date> getWeekDayOfYear(Date date) {
    Calendar calendar = getCalendarFromDate(date);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
    int week = calendar.get(Calendar.WEEK_OF_YEAR);
    int year = calendar.get(Calendar.YEAR);

    List<Date> result = new ArrayList<Date>();
    result.add(getDateOfYearWeek(year, week, Calendar.MONDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.TUESDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.WEDNESDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.THURSDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.FRIDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.SATURDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.SUNDAY));
    return result;
}
 
Example 3
Project: MovingGdufe-Android   File: TimeUtils.java   View Source Code Vote up 6 votes
/**
 * 将日期转为当周的星期一,返回getTime()的long值
 * @param time Date
 * @return long
 * @throws ParseException
 */
private static long convertToMonday(Date time) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
    Calendar cal = Calendar.getInstance();
    cal.setTime(time);
    // 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
    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());	//所在周星期一的日期
    return sdf.parse(imptimeBegin).getTime();
}
 
Example 4
Project: Dahlem_SER316   File: JNCalendar.java   View Source Code Vote up 6 votes
void setCalendarParameters() {
	int d = 1;

	Calendar cal = _date.getCalendar();

	if (Configuration.get("FIRST_DAY_OF_WEEK").equals("mon")) {
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		d = 2;
	} else
		cal.setFirstDayOfWeek(Calendar.SUNDAY);

	cal.set(Calendar.DAY_OF_MONTH, 1);
	cal.getTime();
	firstDay = cal.get(Calendar.DAY_OF_WEEK) - d;
	if (firstDay == -1)
		firstDay = 6;
	daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
 
Example 5
Project: kettle_support_kettle8.0   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 返回该年有多少个自然周
 * 
 * @param year
 *            最多53 一般52 如果12月月末今天在本年53周(属于第二年第一周) 那么按照当年52周算
 * @return
 */
public static int getAllWeeks(String year) {
	Calendar calendar = Calendar.getInstance();
	SimpleDateFormat sdf = getTimeFormatYMD();
	try {
		calendar.setTime(sdf.parse(year + "-12-31"));
	} catch (ParseException e) {
		e.printStackTrace();
	}
	calendar.setFirstDayOfWeek(Calendar.MONDAY);
	int week = calendar.get(Calendar.WEEK_OF_YEAR);
	if (week != 53) {
		week = 52;
	}
	return week;
}
 
Example 6
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 6 votes
@Override
public String format(DateTime timestamp) {
    Calendar c = Calendar.getInstance(timestamp.getZone().toTimeZone(), LOCALE);
    c.setFirstDayOfWeek(Calendar.SUNDAY);
    c.setMinimalDaysInFirstWeek(7);
    c.setTimeInMillis(timestamp.withZone(DateTimeZone.UTC).getMillis());
    return zeroPadded(4, String.valueOf(c.getWeekYear()));
}
 
Example 7
Project: DMS   File: CalendarView.java   View Source Code Vote up 6 votes
public void setWeekAsSelected(Date currentDate) {
    final Calendar currentCalendar = CalendarUtility.getTodayCalendar(mContext, mFirstDayOfWeek);
    currentCalendar.setFirstDayOfWeek(mFirstDayOfWeek);
    currentCalendar.setTime(currentDate);

    // Clear previous marks
    clearWeekOfTheMonthStyle(mLastSelectedDay);

    // Store current values as last values
    setLastSelectedDay(currentDate);

    List<DayView> views = findWeekViewsByCalendar(currentCalendar);
    for (DayView view : views) {
        view.setBackgroundColor(mSelectedDayBackground);
    }
}
 
Example 8
Project: lorne_core   File: DateUtil.java   View Source Code Vote up 6 votes
/**
 * 本周指定星期几的日期
 *
 * @param date  指定的日期
 * @param day   那一天
 * @return  结果
 */
public static String getCurrentWeekDay(Date date, int day) {
    Calendar calendar = getCalendarFromDate(date);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
    int week = calendar.get(Calendar.WEEK_OF_YEAR);
    int year = calendar.get(Calendar.YEAR);

    return formatDate(getDateOfYearWeek(year, week, day));
}
 
Example 9
Project: SkyEye   File: DateUtil.java   View Source Code Vote up 6 votes
/**
 * 返回月的第几周
 * @param day
 * @return
 */
public static String getWeek(String day) {
    String[] ymd = day.split(Constants.MIDDLE_LINE);
    SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
    Date date = null;
    try {
        date = sdf.parse(day);
    } catch (ParseException e) {
        return Constants.EMPTY_STR;
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setTime(date);
    int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
    return ymd[0] + Constants.MIDDLE_LINE + ymd[1] + Constants.COLON + weekOfMonth;
}
 
Example 10
Project: amap   File: MIP_DateUtil.java   View Source Code Vote up 6 votes
/**
 * 取得当前日期是多少周
 * 
 * @param date
 * @return
 */
public static int getWeekOfYear(Date date)
{
    Calendar c = new GregorianCalendar();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setMinimalDaysInFirstWeek(7);
    c.setTime(date);
    
    return c.get(Calendar.WEEK_OF_YEAR);
}
 
Example 11
Project: openjdk-jdk10   File: CalendarRegression.java   View Source Code Vote up 6 votes
/**
 * 4080631: Calendar.hashCode is amazingly bad
 */
public void Test4080631() {
    Calendar cal = Calendar.getInstance();
    int h1 = cal.hashCode();
    cal.add(SECOND, +1);
    int h2 = cal.hashCode();
    Calendar cal2 = (Calendar) cal.clone();
    cal.add(MILLISECOND, +1);
    int h3 = cal.hashCode();
    logln("hash code: h1=" + h1 + ", h2=" + h2 + ", h3=" + h3);
    if (h1 == h2 || h1 == h3 || h2 == h3) {
        errln("hash code is poor: hashCode=" + h1);
    }
    h2 = cal2.hashCode();
    cal.add(MILLISECOND, -1);
    int h4 = cal.hashCode();
    logln("hash code: h2=" + h2 + ", h4=" + h4);
    if (cal.equals(cal2) && h2 != h4) {
        errln("broken hash code: h2=" + h2 + ", h4=" + h4);
    }
    int x = cal.getFirstDayOfWeek() + 3;
    if (x > SATURDAY) {
        x -= 7;
    }
    cal.setFirstDayOfWeek(x);
    int h5 = cal.hashCode();
    logln("hash code: h4=" + h4 + ", h5=" + h5);
    if (h4 == h5) {
        errln("has code is poor with first day of week param: hashCode=" + h4);
    }
}
 
Example 12
Project: SER316-Munich   File: JNCalendar.java   View Source Code Vote up 6 votes
void setCalendarParameters() {
	int d = 1;

	Calendar cal = _date.getCalendar();

	if (Configuration.get("FIRST_DAY_OF_WEEK").equals("mon")) {
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		d = 2;
	} else
		cal.setFirstDayOfWeek(Calendar.SUNDAY);

	cal.set(Calendar.DAY_OF_MONTH, 1);
	cal.getTime();
	firstDay = cal.get(Calendar.DAY_OF_WEEK) - d;
	if (firstDay == -1)
		firstDay = 6;
	daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
 
Example 13
Project: calendarview2   File: CalendarView2.java   View Source Code Vote up 5 votes
private int getWeekCountBasedOnMode() {
    int weekCount = calendarMode.visibleWeeksCount;
    boolean isInMonthsMode = calendarMode.equals(CalendarMode.MONTHS);
    if (isInMonthsMode && mDynamicHeightEnabled && adapter != null && pager != null) {
        Calendar cal = (Calendar) adapter.getItem(pager.getCurrentItem()).getCalendar().clone();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        //noinspection ResourceType
        cal.setFirstDayOfWeek(getFirstDayOfWeek());
        weekCount = cal.get(Calendar.WEEK_OF_MONTH);
    }
    return weekCount + DAY_NAMES_ROW;
}
 
Example 14
Project: GitHub   File: MaterialCalendarView.java   View Source Code Vote up 5 votes
private int getWeekCountBasedOnMode() {
    int weekCount = calendarMode.visibleWeeksCount;
    boolean isInMonthsMode = calendarMode.equals(CalendarMode.MONTHS);
    if (isInMonthsMode && mDynamicHeightEnabled && adapter != null && pager != null) {
        Calendar cal = (Calendar) adapter.getItem(pager.getCurrentItem()).getCalendar().clone();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        //noinspection ResourceType
        cal.setFirstDayOfWeek(getFirstDayOfWeek());
        weekCount = cal.get(Calendar.WEEK_OF_MONTH);
    }
    return weekCount + DAY_NAMES_ROW;
}
 
Example 15
Project: wcs-android-sdk   File: DateUtil.java   View Source Code Vote up 5 votes
public static Date getFirstDayOfMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.set(Calendar.DAY_OF_MONTH, ONE);
    return calendar.getTime();
}
 
Example 16
Project: FriendBook   File: StringUtils.java   View Source Code Vote up 5 votes
/**
 * 获取当前时间为每年第几周
 *
 * @param date
 * @return
 */
public static int getWeekOfYear(Date date) {
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setTime(date);
    int week = c.get(Calendar.WEEK_OF_YEAR) - 1;
    week = week == 0 ? 52 : week;
    return week > 0 ? week : 1;
}
 
Example 17
Project: oscm   File: DateConverter.java   View Source Code Vote up 5 votes
/**
 * Converts the given time to a time in the current time zone representing
 * the end of the week. It is assumed that weeks always start on MONDAY
 * independently of the server locale.
 * 
 * @param baseTime
 *            time in milliseconds to be converted
 * @return Calendar representing the end of the week based on the given
 *         baseTime
 */
public static Calendar getEndOfWeek(long baseTime) {
    final Calendar cal = getCalendar(baseTime);
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 999);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    return cal;
}
 
Example 18
Project: DMS   File: CalendarView.java   View Source Code Vote up 5 votes
private void clearWeekOfTheMonthStyle(Date currentDate) {
        if (currentDate != null) {
            final Calendar calendar = CalendarUtility.getTodayCalendar(mContext, mFirstDayOfWeek);
            calendar.setFirstDayOfWeek(mFirstDayOfWeek);
            calendar.setTime(currentDate);

//            final List<DayView> weekViews = findWeekViewsByCalendar(calendar);
//            for (DayView weekView : weekViews) {
//                weekView.setBackgroundColor(mCalendarBackgroundColor);
//
//                mIsCommonDay = true;
//                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
//                if (totalDayOfWeekend().length != 0) {
//                    for (int weekend : totalDayOfWeekend()) {
//                        if (calendar.get(Calendar.DAY_OF_WEEK) == weekend) {
//                            weekView.setTextColor(mWeekendColor);
//                            mIsCommonDay = false;
//                        }
//                    }
//                }
//
//                if (mIsCommonDay) {
//                    weekView.setTextColor(mDayOfWeekTextColor);
//                }
//            }

            final List<DayView> views = findWeekViewsByCalendar(calendar);
            for (DayView view : views) {
                view.setBackgroundColor(mCalendarBackgroundColor);
            }
        }
    }
 
Example 19
Project: amap   File: MIP_TimeUtils.java   View Source Code Vote up 5 votes
/**
 * 取得指定日期所在周的最后一天
 * 
 * @param date
 * @return
 */
public static Date getLastDayOfWeek(Date date)
{
    Calendar c = new GregorianCalendar();
    c.setFirstDayOfWeek(Calendar.SUNDAY);
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
    return c.getTime();
}
 
Example 20
Project: mvvm-template   File: DatesUtils.java   View Source Code Vote up 3 votes
/**
 * Return if the date given is a first week of mount
 *
 * @param year
 *         The year of the date.
 * @param month
 *         The month of the date.
 * @param day
 *         The day of the date.
 * @return true or false
 */
@SuppressLint("WrongConstant") public static boolean isFirstWeekOfMount(int year, int month, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month - 1, day);
    calendar.add(Calendar.SECOND, 0);
    calendar.setFirstDayOfWeek(Calendar.SUNDAY);

    return calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH) == 1;
}