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.alkacon.opencms.calendar.CmsCalendarMonthBean.java

/**
 * Builds the HTML output to create a basic calendar month overview.<p>
 * // www.ja  va2  s.c  om
 * This method serves as a simple example to create a basic html calendar monthly view.<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 weeks
 * @param showNavigation if true, navigation links to switch the month are created, otherwise not
 * @return the HTML output to create a basic calendar month overview
 */
public String buildCalendarMonth(int year, int month, Locale calendarLocale, boolean showNavigation) {

    StringBuffer result = new StringBuffer(1024);

    Map dates = getMonthDaysMatrix(year, month, calendarLocale);
    Map monthEntries = getEntriesForMonth(year, month);

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

    // init the date format symbols
    DateFormatSymbols calendarSymbols = new DateFormatSymbols(calendarLocale);

    // open the table
    result.append("<table class=\"");
    result.append(getStyle().getStyleTable());
    result.append("\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");

    // create the calendar navigation row
    result.append(buildMonthNavigation(year, month, currentCalendar, calendarLocale, showNavigation));

    // create the week day row
    result.append("<tr>\n");
    int currWeekDay = weekStart;
    for (int i = 1; i <= 7; i++) {

        result.append("\t<td class=\"");
        result.append(getStyle().getStyleWeekdays());
        result.append("\">");
        result.append(calendarSymbols.getShortWeekdays()[currWeekDay]);
        result.append("</td>\n");
        // check if we are at end of week
        if (currWeekDay == Calendar.SATURDAY) {
            currWeekDay = 0;
        }

        currWeekDay++;
    }
    result.append("</tr>\n");

    // now create the entry rows
    result.append("<tr>\n");

    // iterate the index entries of the matrix
    Iterator i = dates.keySet().iterator();
    while (i.hasNext()) {
        Integer index = (Integer) i.next();
        result.append("\t<td class=\"");
        Calendar currDay = (Calendar) dates.get(index);
        if (currDay != null) {
            // current index represents a day, create day output
            String styleDayCell = getStyle().getStyleDay();
            if (isCurrentDay(currentCalendar, currDay)) {
                // for the current day, use another cell style
                styleDayCell = getStyle().getStyleDayCurrent();
            }
            // get entries for the day
            List dayEntries = (List) monthEntries.get(currDay.getTime());
            if (dayEntries.size() > 0) {
                // current day has calendar entries
                int weekdayStatus = 0;
                int holidayEntries = 0;
                int commonEntries = dayEntries.size();
                StringBuffer dayText = new StringBuffer(128);
                // check all entries for special weekday status entries
                for (int k = 0; k < commonEntries; k++) {
                    CmsCalendarEntry entry = (CmsCalendarEntry) dayEntries.get(k);
                    int entryWeekdayStatus = entry.getEntryData().getWeekdayStatus();
                    if (entryWeekdayStatus > 0) {
                        // entry is a special weekday
                        holidayEntries++;
                        // append special day info to title info
                        dayText.append(entry.getEntryData().getTitle());
                        dayText.append(" - ");
                        if (entryWeekdayStatus > weekdayStatus) {
                            // increase the status of the weekday
                            weekdayStatus = entryWeekdayStatus;
                        }
                    }
                }
                // calculate the count of common calendar entries
                commonEntries = commonEntries - holidayEntries;
                // determine the CSS class to use
                String dayStyle = getWeekdayStyle(currDay.get(Calendar.DAY_OF_WEEK), weekdayStatus);
                result.append(styleDayCell);
                result.append("\" title=\"");
                result.append(dayText);
                // check the number of common entries and generate output of entry count
                if (commonEntries <= 0) {
                    // no entry found
                    result.append(getMessages().key("calendar.entries.count.none"));
                } else if (commonEntries == 1) {
                    // one entry found
                    result.append(getMessages().key("calendar.entries.count.one"));
                } else {
                    // more than one entry found
                    result.append(getMessages().key("calendar.entries.count.more",
                            new String[] { String.valueOf(commonEntries) }));
                }
                result.append("\">");
                if (commonEntries > 0) {
                    // common entries present, create link to the overview page 
                    result.append("<a href=\"");
                    result.append(createLink(currDay, m_viewUri, true, -1));
                    result.append("\" class=\"");
                    result.append(getStyle().getStyleDayEntryLink());
                    result.append("\">");
                }
                result.append("<span class=\"");
                result.append(dayStyle);
                result.append("\">");
                result.append(currDay.get(Calendar.DAY_OF_MONTH));
                result.append("</span>");
                if (commonEntries > 0) {
                    // common entries present, close link
                    result.append("</a>");
                }
            } else {
                // current day has no entries
                result.append(styleDayCell);
                result.append("\" title=\"");
                result.append(getMessages().key("calendar.entries.count.none"));
                result.append("\">");
                result.append("<span class=\"");
                result.append(getWeekdayStyle(currDay.get(Calendar.DAY_OF_WEEK),
                        I_CmsCalendarEntryData.WEEKDAYSTATUS_WORKDAY));
                result.append("\">");
                result.append(currDay.get(Calendar.DAY_OF_MONTH));
                result.append("</span>");
            }
        } else {
            // this is an empty cell
            result.append(getStyle().getStyleDayEmpty());
            result.append("\">");
        }
        result.append("</td>\n");
        if ((index.intValue() % 7) == 0) {
            // append closing row tag
            result.append("</tr>\n");
            if (i.hasNext()) {
                // open next row if more elements are present
                result.append("<tr>\n");
            }
        }
    }

    // close the table
    result.append("</table>");
    return result.toString();
}

From source file:org.yes.cart.web.page.component.customer.order.CustomerOrderPanel.java

private Date determineDate(final PageParameters pageParameters) {
    String viewTime = pageParameters.get("view").toString();
    if (viewTime == null || !SUPPORTED_VIEWS.contains(viewTime)) {
        viewTime = DEFAULT_VIEW;/*w ww  . j  a v a  2 s  .c o  m*/
    }

    if ("all".equals(viewTime)) {
        return null;
    }

    final Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    if ("month".equals(viewTime)) {

        calendar.set(Calendar.DAY_OF_MONTH, 1);

    } else if ("year".equals(viewTime)) {

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.MONTH, 0);

    } else {

        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());

        final Calendar now = Calendar.getInstance();
        if (now.before(calendar)) {
            calendar.add(Calendar.DATE, -7);
        }

    }

    return calendar.getTime();
}

From source file:com.marcohc.robotocalendar.RobotoCalendarView.java

private int getWeekIndex(int weekIndex, Calendar currentCalendar) {
    int firstDayWeekPosition = currentCalendar.getFirstDayOfWeek();

    if (firstDayWeekPosition == 1) {
        return weekIndex;
    } else {//w  ww.j a  va  2  s.  c o m

        if (weekIndex == 1) {
            return 7;
        } else {
            return weekIndex - 1;
        }
    }
}

From source file:org.jfree.data.time.Week.java

/**
 * Returns the first millisecond of the week, evaluated using the supplied
 * calendar (which determines the time zone).
 *
 * @param calendar  the calendar (<code>null</code> not permitted).
 *
 * @return The first millisecond of the week.
 *
 * @throws NullPointerException if <code>calendar</code> is
 *     <code>null</code>.//from  ww  w . j a v a2  s.com
 */
@Override
public long getFirstMillisecond(Calendar calendar) {
    Calendar c = (Calendar) calendar.clone();
    c.clear();
    c.set(Calendar.YEAR, this.year);
    c.set(Calendar.WEEK_OF_YEAR, this.week);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTimeInMillis();
}

From source file:org.jfree.data.time.Week.java

/**
 * Returns the last millisecond of the week, evaluated using the supplied
 * calendar (which determines the time zone).
 *
 * @param calendar  the calendar (<code>null</code> not permitted).
 *
 * @return The last millisecond of the week.
 *
 * @throws NullPointerException if <code>calendar</code> is
 *     <code>null</code>./*from  w w  w  .j a  va  2s  . c om*/
 */
@Override
public long getLastMillisecond(Calendar calendar) {
    Calendar c = (Calendar) calendar.clone();
    c.clear();
    c.set(Calendar.YEAR, this.year);
    c.set(Calendar.WEEK_OF_YEAR, this.week + 1);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTimeInMillis() - 1;
}

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

public static Timestamp getWeekStart(Timestamp stamp, int daysLater, int weeksLater, TimeZone timeZone,
        Locale locale) {/*from   ww w.  j a  v a 2  s  . c  o  m*/
    Calendar tempCal = toCalendar(stamp, timeZone, locale);
    tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0,
            0, 0);
    tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
    tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek());
    tempCal.add(Calendar.WEEK_OF_MONTH, weeksLater);
    Timestamp retStamp = new Timestamp(tempCal.getTimeInMillis());
    retStamp.setNanos(0);
    return retStamp;
}

From source file:org.richfaces.component.UICalendar.java

protected int getDefaultFirstWeekDay() {
    Calendar cal = getCalendar();
    return cal.getFirstDayOfWeek() - cal.getActualMinimum(Calendar.DAY_OF_WEEK);
}

From source file:io.apiman.gateway.engine.jdbc.JdbcMetrics.java

/**
 * Process the next item in the queue./* w w w . j a  va  2 s.c o m*/
 */
@SuppressWarnings("nls")
protected void processQueue() {
    try {
        RequestMetric metric = queue.take();
        QueryRunner run = new QueryRunner(ds);

        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("UTC"));
        cal.setTime(metric.getRequestStart());

        long rstart = cal.getTimeInMillis();
        long rend = metric.getRequestEnd().getTime();
        long duration = metric.getRequestDuration();
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        long minute = cal.getTimeInMillis();
        cal.set(Calendar.MINUTE, 0);
        long hour = cal.getTimeInMillis();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        long day = cal.getTimeInMillis();
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        long week = cal.getTimeInMillis();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        long month = cal.getTimeInMillis();
        String api_org_id = metric.getApiOrgId();
        String api_id = metric.getApiId();
        String api_version = metric.getApiVersion();
        String client_org_id = metric.getClientOrgId();
        String client_id = metric.getClientId();
        String client_version = metric.getClientVersion();
        String plan = metric.getPlanId();
        String user_id = metric.getUser();
        String rtype = null;
        if (metric.isFailure()) {
            rtype = "failure";
        } else if (metric.isError()) {
            rtype = "error";
        }
        long bytes_up = metric.getBytesUploaded();
        long bytes_down = metric.getBytesDownloaded();

        // Now insert a row for the metric.
        run.update("INSERT INTO gw_requests (" + "rstart, rend, duration, month, week, day, hour, minute, "
                + "api_org_id, api_id, api_version, " + "client_org_id, client_id, client_version, plan, "
                + "user_id, resp_type, bytes_up, bytes_down) VALUES (" + "?, ?, ?, ?, ?, ?, ?, ?," + "?, ?, ?,"
                + "?, ?, ?, ?," + "?, ?, ?, ?)", rstart, rend, duration, month, week, day, hour, minute,
                api_org_id, api_id, api_version, client_org_id, client_id, client_version, plan, user_id, rtype,
                bytes_up, bytes_down);
    } catch (InterruptedException ie) {
        // This means that the thread was stopped.
    } catch (Exception e) {
        // TODO better logging of this unlikely error
        System.err.println("Error adding metric to database:"); //$NON-NLS-1$
        e.printStackTrace();
        return;
    }
}

From source file:com.marcohc.robotocalendar.RobotoCalendarView.java

private int getMonthOffset(Calendar currentCalendar) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(currentCalendar.getTime());
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    int firstDayWeekPosition = calendar.getFirstDayOfWeek();
    int dayPosition = calendar.get(Calendar.DAY_OF_WEEK);

    if (firstDayWeekPosition == 1) {
        return dayPosition - 1;
    } else {/*w ww . j  a v a2s  .c o m*/

        if (dayPosition == 1) {
            return 6;
        } else {
            return dayPosition - 2;
        }
    }
}

From source file:DateChooserPanel.java

/**
 * Constructs a new date chooser panel.// w  w w. j av  a  2s. co  m
 *
 * @param calendar     the calendar controlling the date.
 * @param controlPanel a flag that indicates whether or not the 'today' 
 *                     button should appear on the panel.
 */
public DateChooserPanel(final Calendar calendar, final boolean controlPanel) {

    super(new BorderLayout());

    this.chosenDateButtonColor = UIManager.getColor("textHighlight");
    this.chosenMonthButtonColor = UIManager.getColor("control");
    this.chosenOtherButtonColor = UIManager.getColor("controlShadow");

    // the default date is today...
    this.chosenDate = calendar;
    this.firstDayOfWeek = calendar.getFirstDayOfWeek();
    this.WEEK_DAYS = new int[7];
    for (int i = 0; i < 7; i++) {
        this.WEEK_DAYS[i] = ((this.firstDayOfWeek + i - 1) % 7) + 1;
    }

    add(constructSelectionPanel(), BorderLayout.NORTH);
    add(getCalendarPanel(), BorderLayout.CENTER);
    if (controlPanel) {
        add(constructControlPanel(), BorderLayout.SOUTH);
    }
    setDate(calendar.getTime());
}