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:fr.paris.lutece.plugins.suggest.utils.SuggestUtils.java

/**
 * return the first day of week function of the date .
 * @param date the date/*from   ww  w. ja  v  a  2s  .  c o m*/
 * @return the first day of week function of the date.
 */
public static Timestamp getFirstDayOfWeek(Timestamp date) {
    Calendar caldate = new GregorianCalendar();
    caldate.setTime(date);
    caldate.set(Calendar.MILLISECOND, caldate.getActualMinimum(Calendar.MILLISECOND));
    caldate.set(Calendar.SECOND, caldate.getActualMinimum(Calendar.SECOND));
    caldate.set(Calendar.HOUR_OF_DAY, caldate.getActualMinimum(Calendar.HOUR_OF_DAY));
    caldate.set(Calendar.MINUTE, caldate.getActualMinimum(Calendar.MINUTE));
    caldate.set(Calendar.DAY_OF_WEEK, caldate.getFirstDayOfWeek());

    Timestamp timeStamp = new Timestamp(caldate.getTimeInMillis());

    return timeStamp;
}

From source file:fr.paris.lutece.plugins.suggest.utils.SuggestUtils.java

/**
 * return the last day of week function of the date .
 * @param date the date//from   www  .ja  v a2s .  co  m
 * @return the last day of week function of the date.
 */
public static Timestamp getLastDayOfWeek(Timestamp date) {
    Calendar caldate = new GregorianCalendar();
    caldate.setTime(date);
    caldate.set(Calendar.MILLISECOND, caldate.getActualMaximum(Calendar.MILLISECOND));
    caldate.set(Calendar.SECOND, caldate.getActualMaximum(Calendar.SECOND));
    caldate.set(Calendar.HOUR_OF_DAY, caldate.getActualMaximum(Calendar.HOUR_OF_DAY));
    caldate.set(Calendar.MINUTE, caldate.getActualMaximum(Calendar.MINUTE));
    caldate.set(Calendar.DAY_OF_WEEK, caldate.getFirstDayOfWeek() + 6);

    Timestamp timeStamp = new Timestamp(caldate.getTimeInMillis());

    return timeStamp;
}

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

/**
* ?/*from  w w  w . j av  a2  s  .  c  o  m*/
* 
*/
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:com.baidu.rigel.biplatform.tesseract.meta.impl.TimeDimensionMemberServiceImpl.java

/**
 * ??/*from   w w w.j  av a2  s .  co  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.alkacon.opencms.calendar.CmsSerialDateWidget.java

/**
 * Returns the HTML for a select box for choosing the week day.<p>
 * /*from   w  w  w  .j a v  a2  s .  co  m*/
 * @param name the name of the select box
 * @param parameters optional additional parameters
 * @param messages localized messages for localizing the options
 * @param selectedWeekday the selected week day number
 * @return the HTML for a select box for choosing the week day
 */
protected String buildSelectWeekDay(String name, String parameters, CmsMessages messages, int selectedWeekday) {

    SimpleDateFormat df = new SimpleDateFormat("EEEE", messages.getLocale());
    Calendar cal = new GregorianCalendar(messages.getLocale());
    List options = new ArrayList(7);
    List values = new ArrayList(7);
    int selectedIndex = 0;
    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    for (int i = 0; i < 7; i++) {
        // iterate the week days and check which one is selected
        values.add(String.valueOf(cal.get(Calendar.DAY_OF_WEEK)));
        options.add(df.format(cal.getTime()));
        if (selectedWeekday == cal.get(Calendar.DAY_OF_WEEK)) {
            selectedIndex = i;
        }
        cal.add(Calendar.DAY_OF_WEEK, 1);
    }
    // add the name to the parameters
    if (CmsStringUtil.isNotEmpty(parameters)) {
        parameters += " ";
    }
    parameters += "name=\"" + name + "\"";
    return CmsWorkplace.buildSelect(parameters, options, values, selectedIndex, true);
}

From source file:org.apache.click.extras.control.DateField.java

/**
 * Return the first day of the week. For example e.g., Sunday in US,
 * Monday in France and Australia.//w ww  . jav  a2s. c o m
 *
 * @return the first day of the week
 */
protected int getFirstDayOfWeek() {
    Locale locale = getLocale();

    if ("AU".equals(locale.getCountry())) {
        return Calendar.MONDAY;
    }
    Calendar calendar = Calendar.getInstance(locale);

    return calendar.getFirstDayOfWeek();
}

From source file:com.alkacon.opencms.v8.calendar.CmsSerialDateWidget.java

/**
 * Returns the HTML for a select box for choosing the week day.<p>
 * /*from   w w w. ja va  2s.c o m*/
 * @param name the name of the select box
 * @param parameters optional additional parameters
 * @param messages localized messages for localizing the options
 * @param selectedWeekday the selected week day number
 * @return the HTML for a select box for choosing the week day
 */
protected String buildSelectWeekDay(String name, String parameters, CmsMessages messages, int selectedWeekday) {

    SimpleDateFormat df = new SimpleDateFormat("EEEE", messages.getLocale());
    Calendar cal = new GregorianCalendar(messages.getLocale());
    List<String> options = new ArrayList<String>(7);
    List<String> values = new ArrayList<String>(7);
    int selectedIndex = 0;
    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    for (int i = 0; i < 7; i++) {
        // iterate the week days and check which one is selected
        values.add(String.valueOf(cal.get(Calendar.DAY_OF_WEEK)));
        options.add(df.format(cal.getTime()));
        if (selectedWeekday == cal.get(Calendar.DAY_OF_WEEK)) {
            selectedIndex = i;
        }
        cal.add(Calendar.DAY_OF_WEEK, 1);
    }
    // add the name to the parameters
    if (CmsStringUtil.isNotEmpty(parameters)) {
        parameters += " ";
    }
    parameters += "name=\"" + name + "\"";
    return CmsWorkplace.buildSelect(parameters, options, values, selectedIndex, true);
}

From source file:org.sakaiproject.tool.summarycalendar.ui.CalendarBean.java

private List getWeek() {
    if (reloadCalendarEvents()) {
        // initialize days
        weeks = new ArrayList();
        for (int d = 0; d < 7; d++) {
            week1.setDay(d, new Day());
        }/*  ww  w. ja  va 2s  .c o  m*/
        weeks.add(week1);

        // selected week
        Calendar c = Calendar.getInstance(getCurrentUserTimezone(), msgs.getLocale());
        c.setTime(getViewingDate());
        int selMonth = c.get(Calendar.MONTH);
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        //int selWeek = c.get(Calendar.WEEK_OF_YEAR);

        // select first day of week (locale-specific)
        while (dayOfWeek != c.getFirstDayOfWeek()) {
            c.add(Calendar.DAY_OF_WEEK, -1);
            dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        }

        for (int i = 0; i < 7; i++) {
            Day day;
            boolean sameMonth = (selMonth == c.get(Calendar.MONTH));
            boolean selected = (selectedDay != null) && (sameDay(c, selectedDay));

            CalendarEventVector vector = getScheduleEventsForDay(c);
            day = new Day(c, getDayEventCount(vector) > 0);
            day.setOccursInOtherMonth(!sameMonth);
            day.setBackgroundCSSProperty(getDayPriorityCSSProperty(vector));
            day.setToday(sameDay(c, getToday()));
            day.setSelected(selected);
            day.setDayEvents(getDayEvents(vector));

            week1.setDay(i, day);
            c.add(Calendar.DAY_OF_WEEK, +1);
        }
        weeks.set(0, week1);
    }
    return weeks;
}

From source file:com.autentia.jsf.component.ocupation.HtmlOcupationCalendarRenderer.java

@Override
public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
    log.debug("encodeEnd - component=\"" + component + "\".");
    RendererUtils.checkParamValidity(facesContext, component, HtmlOcupationCalendar.class);

    HtmlOcupationCalendar inputCalendar = (HtmlOcupationCalendar) component;

    Locale currentLocale = facesContext.getViewRoot().getLocale();

    Date value = null;/*from w ww  .  java2s . c o  m*/

    Calendar timeKeeper = Calendar.getInstance(currentLocale);
    timeKeeper.setTime(new Date());

    try {
        value = new CalendarDateTimeConverter().getAsDate(facesContext, inputCalendar);
        timeKeeper.setTime(value);
    } catch (IllegalArgumentException i) {
        log.warn("encodeEnd - IllegalArgumentException", i);
    }

    if (log.isDebugEnabled()) {
        log.debug("encodeEnd - timeKeeper=\"" + timeKeeper.getTime() + "\".");
    }

    DateFormatSymbols symbols = new DateFormatSymbols(currentLocale);

    String[] weekdays = mapShortWeekdays(symbols);

    int lastDayInMonth = timeKeeper.getActualMaximum(Calendar.DAY_OF_MONTH);

    int currentDay = timeKeeper.get(Calendar.DAY_OF_MONTH);

    if (currentDay > lastDayInMonth)
        currentDay = lastDayInMonth;

    timeKeeper.set(Calendar.DAY_OF_MONTH, 1);

    int weekDayOfFirstDayOfMonth = mapCalendarDayToCommonDay(timeKeeper.get(Calendar.DAY_OF_WEEK));

    int weekStartsAtDayIndex = mapCalendarDayToCommonDay(timeKeeper.getFirstDayOfWeek());

    ResponseWriter writer = facesContext.getResponseWriter();

    HtmlRendererUtils.writePrettyLineSeparator(facesContext);
    HtmlRendererUtils.writePrettyLineSeparator(facesContext);

    // render table
    writer.startElement(HTML.TABLE_ELEM, component);

    HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.UNIVERSAL_ATTRIBUTES);
    HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.EVENT_HANDLER_ATTRIBUTES);
    writer.flush();

    HtmlRendererUtils.writePrettyLineSeparator(facesContext);

    // render days
    writeDays(facesContext, writer, inputCalendar, timeKeeper, currentDay, weekStartsAtDayIndex,
            weekDayOfFirstDayOfMonth, lastDayInMonth, weekdays);

    writer.endElement(HTML.TABLE_ELEM);
}

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

/**
 * Returns all displayed days of the specified week with their corresponding entries as lists.<p>
 * /*from   w ww. j  a v  a 2s. c o m*/
 * The key of the Map has to be a Date object.<p>
 * 
 * The Map values are always lists of {@link CmsCalendarEntry} objects, if no entries are available for a specific day,
 * an empty List is returned.<p>
 * 
 * @param year the year of the month to display
 * @param week the week of the year to display
 * @return all displayed days of the specified day range with their corresponding entries as lists
 */
public Map getEntriesForWeek(int year, int week) {

    Calendar startDay = new GregorianCalendar(getJsp().getRequestContext().getLocale());
    startDay.set(Calendar.YEAR, year);
    startDay.set(Calendar.WEEK_OF_YEAR, week);
    startDay.set(Calendar.DAY_OF_WEEK, startDay.getFirstDayOfWeek());

    Calendar endDay = (Calendar) startDay.clone();
    endDay.add(Calendar.DAY_OF_YEAR, 6);

    return getEntriesForDays(startDay, endDay);
}