Example usage for java.util Calendar WEEK_OF_MONTH

List of usage examples for java.util Calendar WEEK_OF_MONTH

Introduction

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

Prototype

int WEEK_OF_MONTH

To view the source code for java.util Calendar WEEK_OF_MONTH.

Click Source Link

Document

Field number for get and set indicating the week number within the current month.

Usage

From source file:com.searchcode.app.App.java

/**
 * Used to know what week of the month it is to display a different image on the main page
 *//* w w  w .  jav a 2  s.  c  o  m*/
private static int getWeekOfMonth() {
    Calendar cal = Calendar.getInstance();
    return cal.get(Calendar.WEEK_OF_MONTH);
}

From source file:com.icesoft.faces.component.selectinputdate.SelectInputDateRenderer.java

private void writeDays(DOMContext domContext, FacesContext facesContext, ResponseWriter writer,
        SelectInputDate inputComponent, Calendar timeKeeper, int currentDay, int weekStartsAtDayIndex,
        int weekDayOfFirstDayOfMonth, int lastDayInMonth, Element table, String[] months, String[] weekdays,
        String[] weekdaysLong, Converter converter, Date value) throws IOException {
    Calendar cal;/*from  w ww  . ja v a 2s . c  om*/

    int space = (weekStartsAtDayIndex < weekDayOfFirstDayOfMonth)
            ? (weekDayOfFirstDayOfMonth - weekStartsAtDayIndex)
            : (weekdays.length - weekStartsAtDayIndex + weekDayOfFirstDayOfMonth);

    if (space == weekdays.length) {
        space = 0;
    }

    int columnIndexCounter = 0;

    Element tr1 = null;
    for (int i = 0; i < space; i++) {
        if (columnIndexCounter == 0) {
            tr1 = domContext.createElement(HTML.TR_ELEM);
            table.appendChild(tr1);
            if (inputComponent.isRenderWeekNumbers()) {
                cal = copyCalendar(facesContext, timeKeeper);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                Element td = domContext.createElement(HTML.TD_ELEM);
                td.setAttribute(HTML.CLASS_ATTR, Util.getQualifiedStyleClass(inputComponent,
                        CSS_DEFAULT.DEFAULT_WEEK_NUM_CLASS, inputComponent.isDisabled()));
                tr1.appendChild(td);
                Text text = domContext.createTextNode(String.valueOf(cal.get(Calendar.WEEK_OF_YEAR)));
                td.appendChild(text);
            }
        }

        writeCell(domContext, facesContext, writer, inputComponent, NBSP, null,
                inputComponent.getDayCellClass(), tr1, null, (weekStartsAtDayIndex + i) % 7, timeKeeper, months,
                weekdaysLong, converter);
        columnIndexCounter++;
    }

    Element tr2 = null;
    for (int i = 0; i < lastDayInMonth; i++) {
        if (columnIndexCounter == 0) {
            // don't create a new row until we have finished the last
            tr2 = domContext.createElement(HTML.TR_ELEM);
            table.appendChild(tr2);
        }

        cal = copyCalendar(facesContext, timeKeeper);
        cal.set(Calendar.DAY_OF_MONTH, i + 1); // i starts at 0 DAY_OF_MONTH start at 1

        // get day, month and year
        // use these to check if the currentDayCell style class should be used
        int day = 0;
        int month = 0;
        int year = 0;
        try {
            Calendar current = copyCalendar(facesContext, timeKeeper);
            current.setTime(value);

            day = current.get(Calendar.DAY_OF_MONTH); // starts with 1
            month = current.get(Calendar.MONTH); // starts with 0
            year = current.get(Calendar.YEAR);
        } catch (Exception e) {
            // hmmm this should never happen
        }
        if (inputComponent.isRenderWeekNumbers() && columnIndexCounter == 0) {
            Element td = domContext.createElement(HTML.TD_ELEM);
            td.setAttribute(HTML.CLASS_ATTR, Util.getQualifiedStyleClass(inputComponent,
                    CSS_DEFAULT.DEFAULT_WEEK_NUM_CLASS, inputComponent.isDisabled()));
            tr2.appendChild(td);
            Text text = domContext.createTextNode(String.valueOf(cal.get(Calendar.WEEK_OF_YEAR)));
            td.appendChild(text);
        }

        if (inputComponent.getHightlightRules().size() > 0) {
            int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
            int weekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
            int date = cal.get(Calendar.DATE);
            int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            int dayOfWeekInMonth = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);

            if (inputComponent.getHightlightRules().containsKey(Calendar.WEEK_OF_YEAR + "$" + weekOfYear)) {
                inputComponent.addHighlightWeekClass(String.valueOf(
                        inputComponent.getHightlightRules().get(Calendar.WEEK_OF_YEAR + "$" + weekOfYear)));
            }
            if (inputComponent.getHightlightRules().containsKey(Calendar.WEEK_OF_MONTH + "$" + weekOfMonth)) {
                inputComponent.addHighlightWeekClass(String.valueOf(
                        inputComponent.getHightlightRules().get(Calendar.WEEK_OF_MONTH + "$" + weekOfMonth)));
            }
            if (inputComponent.getHightlightRules().containsKey(Calendar.DATE + "$" + date)) {
                inputComponent.addHighlightDayClass(
                        String.valueOf(inputComponent.getHightlightRules().get(Calendar.DATE + "$" + date)));
            }
            if (inputComponent.getHightlightRules().containsKey(Calendar.DAY_OF_YEAR + "$" + dayOfYear)) {
                inputComponent.addHighlightDayClass(String.valueOf(
                        inputComponent.getHightlightRules().get(Calendar.DAY_OF_YEAR + "$" + dayOfYear)));
            }
            if (inputComponent.getHightlightRules().containsKey(Calendar.DAY_OF_WEEK + "$" + dayOfWeek)) {
                inputComponent.addHighlightDayClass(String.valueOf(
                        inputComponent.getHightlightRules().get(Calendar.DAY_OF_WEEK + "$" + dayOfWeek)));
            }
            if (inputComponent.getHightlightRules()
                    .containsKey(Calendar.DAY_OF_WEEK_IN_MONTH + "$" + dayOfWeekInMonth)) {
                inputComponent.addHighlightDayClass(String.valueOf(inputComponent.getHightlightRules()
                        .get(Calendar.DAY_OF_WEEK_IN_MONTH + "$" + dayOfWeekInMonth)));
            }
        }

        String cellStyle = CSSNamePool
                .get(inputComponent.getDayCellClass() + " " + inputComponent.getHighlightDayCellClass());

        if ((cal.get(Calendar.DAY_OF_MONTH) == day) && (cal.get(Calendar.MONTH) == month)
                && (cal.get(Calendar.YEAR) == year)) {
            cellStyle = inputComponent.getCurrentDayCellClass();
        }

        // do not automatically select date when navigating by month
        if ((cal.get(Calendar.DAY_OF_MONTH) == day) && (cal.get(Calendar.MONTH) == month)
                && (cal.get(Calendar.YEAR) == year)) {
            cellStyle = inputComponent.getCurrentDayCellClass();
        }

        if (tr2 == null) {
            // finish the first row
            writeCell(domContext, facesContext, writer, inputComponent, String.valueOf(i + 1), cal.getTime(),
                    cellStyle, tr1, null, i, timeKeeper, months, weekdaysLong, converter);
        } else {
            // write to new row
            writeCell(domContext, facesContext, writer, inputComponent, String.valueOf(i + 1), cal.getTime(),
                    cellStyle, tr2, null, i, timeKeeper, months, weekdaysLong, converter);
        }

        columnIndexCounter++;

        if (columnIndexCounter == weekdays.length) {
            columnIndexCounter = 0;
        }
        inputComponent.resetHighlightClasses(Calendar.WEEK_OF_YEAR);
    }

    if ((columnIndexCounter != 0) && (tr2 != null)) {
        for (int i = columnIndexCounter; i < weekdays.length; i++) {
            writeCell(domContext, facesContext, writer, inputComponent, NBSP, null,
                    inputComponent.getDayCellClass(), tr2, null, (weekStartsAtDayIndex + i) % 7, timeKeeper,
                    months, weekdaysLong, converter);
        }
    }

}

From source file:com.projity.contrib.calendar.JXXMonthView.java

/**
 * Returns the bounding box for drawing a date. It is assumed that the
 * calendar, _cal, is already set to the date you want to find the offset
 * for.//from   w  w  w  .  j a  v a  2  s  .c  om
 *
 * @param bounds
 *            Bounds of the date to draw in.
 * @return Point X/Y coordinate to the upper left corner of the bounding box
 *         for date.
 */
private void calculateBoundsForDay(Rectangle bounds, Calendar cal, boolean weekDay) {
    //      PROJITY_MODIFICATION
    int year;
    int month;
    int dayOfWeek;
    int weekOfMonth;
    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH);
    dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    weekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);

    // Determine what row/column we are in.
    int diffMonths = month - _firstDisplayedMonth + ((year - _firstDisplayedYear) * 12);
    int calRowIndex = diffMonths / _numCalCols;

    int calColIndex = diffMonths - (calRowIndex * _numCalCols);
    // Modify the index relative to the first day of the week.
    bounds.x = dayOfWeek - _firstDayOfWeek;
    if (bounds.x < 0) {
        bounds.x += DAYS_IN_WEEK;
    }

    // Offset for location of the day in the week.
    bounds.x = _ltr ? bounds.x * (_boxPaddingX + _boxWidth + _boxPaddingX)
            : (bounds.x + 1) * (_boxPaddingX + _boxWidth + _boxPaddingX);

    // Offset for the column the calendar is displayed in.
    bounds.x += calColIndex * (_calendarWidth + CALENDAR_SPACING);

    // Adjust by centering value.
    bounds.x = _ltr ? _startX + bounds.x : _startX - bounds.x;

    if (weekDay) { // if drawing weekdays, need to draw on the top line
        weekOfMonth = 0;
    }

    // Initial offset for Month and Days of the Week display.
    bounds.y = 2 * (_boxPaddingY + _boxHeight + _boxPaddingY);

    // Offset for centering and row the calendar is displayed in.
    bounds.y += _startY + calRowIndex * (_calendarHeight + CALENDAR_SPACING);

    // Offset for Week of the Month.
    bounds.y += (weekOfMonth - 1) * (_boxPaddingY + _boxHeight + _boxPaddingY);

    //      System.out.println(cal.getTime() + " week of month " + weekOfMonth + " bounds y " + bounds.y );

    bounds.width = _boxPaddingX + _boxWidth + _boxPaddingX;
    bounds.height = _boxPaddingY + _boxHeight + _boxPaddingY;
}

From source file:es.sm2.openppm.front.servlets.ProjectPlanServlet.java

/**
  * Update Staffing Table/*ww  w .j av a2 s .  co m*/
  * @param req
  * @param resp
  * @throws IOException 
  * @throws ServletException 
  */
private void updateStaffingTableJX(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    int idProject = ParamUtil.getInteger(req, Project.IDPROJECT);
    Date since = ParamUtil.getDate(req, "since", getDateFormat(req));
    Date until = ParamUtil.getDate(req, "until", getDateFormat(req));
    boolean showDisabled = ParamUtil.getBoolean(req, Configurations.FILTER_DISABLED, false);

    List<TeamMembersFTEs> ftEs = null;
    List<String> listDates = new ArrayList<String>();

    try {
        TeamMemberLogic teamMemberLogic = new TeamMemberLogic();

        SimpleDateFormat dfYear = new SimpleDateFormat("yy");

        Calendar sinceCal = DateUtil.getCalendar();
        Calendar untilCal = DateUtil.getCalendar();

        sinceCal.setTime(DateUtil.getFirstWeekDay(since));
        untilCal.setTime(DateUtil.getLastWeekDay(until));

        List<Teammember> teammembers = teamMemberLogic.consStaffinFtes(new Project(idProject), since, until,
                showDisabled);

        ftEs = generateFTEsMembersByProject(teammembers, sinceCal.getTime(), untilCal.getTime());

        while (!sinceCal.after(untilCal)) {

            int sinceDay = sinceCal.get(Calendar.DAY_OF_MONTH);
            Calendar calWeek = DateUtil.getLastWeekDay(sinceCal);
            int untilDay = calWeek.get(Calendar.DAY_OF_MONTH);

            listDates.add(sinceDay + "-" + untilDay + " "
                    + getResourceBundle(req).getString("month.min_" + (calWeek.get(Calendar.MONTH) + 1)) + " "
                    + dfYear.format(calWeek.getTime()));

            sinceCal.add(Calendar.WEEK_OF_MONTH, 1);
        }

        // Find configurations
        HashMap<String, String> configurations = RequestUtil.getConfigurationValues(req,
                Configurations.FILTER_DISABLED);

        // Save configuration
        ConfigurationLogic configurationLogic = new ConfigurationLogic();
        configurationLogic.saveConfigurations(getUser(req), configurations,
                Configurations.TYPE_CAPACITY_PLANNING);

    } catch (Exception e) {
        ExceptionUtil.evalueException(req, getResourceBundle(req), LOGGER, e);
    }

    req.setAttribute("listDates", listDates);
    req.setAttribute("ftEs", ftEs);

    forward("/project/common/staffing_table.ajax.jsp", req, resp);
}

From source file:gskproject.Analyze.java

private void ddDTimePeriodActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ddDTimePeriodActionPerformed
    Object obj = evt.getSource();
    if (obj == ddDTimePeriod) {
        if (ddDTimePeriod.getSelectedIndex() == 1) {
            Date[] array = getLastWeek();
            if (departmentAccidentTableLoad(array[0], array[1])) {
                Calendar c = Calendar.getInstance();
                c.setTime(array[0]);/*  w w  w .j  a  v  a 2 s .  c o  m*/
                SimpleDateFormat f = new SimpleDateFormat("MMMMMMM");
                lblTime.setText("For Last Week (Week-" + c.get(Calendar.WEEK_OF_MONTH) + " of "
                        + f.format(array[0]) + ")");
                dtDAFrom.setDate(null);
                dtDATo.setDate(null);
            } else {
                resetFields();
            }

        } else if (ddDTimePeriod.getSelectedIndex() == 2) {
            Date[] array = getLastMonth();
            if (departmentAccidentTableLoad(array[0], array[1])) {
                SimpleDateFormat f = new SimpleDateFormat("MMMMMMM");
                lblTime.setText("For Last Month (" + f.format(array[0]) + ")");
                dtDAFrom.setDate(null);
                dtDATo.setDate(null);
            } else {
                resetFields();
            }

        } else if (ddDTimePeriod.getSelectedIndex() == 3) {
            Date[] array = getLastYear();
            if (departmentAccidentTableLoad(array[0], array[1])) {
                SimpleDateFormat f = new SimpleDateFormat("yyyy");
                lblTime.setText("For Last Year (" + f.format(array[0]) + ")");
                dtDAFrom.setDate(null);
                dtDATo.setDate(null);
            } else {
                resetFields();
            }
        } else if (ddDTimePeriod.getSelectedIndex() == 4) {

            Date from = getThisWeek();
            System.out.println(from);
            Date to = new Date();
            if (departmentAccidentTableLoad(from, to)) {
                Calendar c = Calendar.getInstance();
                c.setTime(from);
                SimpleDateFormat f = new SimpleDateFormat("MMMMMMM");
                lblTime.setText(
                        "For This Week (Week-" + c.get(Calendar.WEEK_OF_MONTH) + " of " + f.format(from) + ")");
                dtDAFrom.setDate(null);
                dtDATo.setDate(null);
            } else {
                resetFields();
            }
        } else if (ddDTimePeriod.getSelectedIndex() == 5) {
            Date from = getThisMonth();
            Date to = new Date();
            if (departmentAccidentTableLoad(from, to)) {
                SimpleDateFormat f = new SimpleDateFormat("MMMMMMM");
                lblTime.setText("For This Month (" + f.format(from) + ")");
                dtDAFrom.setDate(null);
                dtDATo.setDate(null);
            } else {
                resetFields();
            }

        } else if (ddDTimePeriod.getSelectedIndex() == 6) {
            Date from = getThisYear();
            Date to = new Date();
            if (departmentAccidentTableLoad(from, to)) {
                SimpleDateFormat f = new SimpleDateFormat("yyyy");
                lblTime.setText("For This Year (" + f.format(from) + ")");
                dtDAFrom.setDate(null);
                dtDATo.setDate(null);
            } else {
                resetFields();
            }
        }
    }
}

From source file:phenoviewer.ReNameJFrame.java

private String replaceTokens(Date date, int counter, String path) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);/*from   w ww  . j a v a  2 s.com*/

    Map<String, Object> tokens = new HashMap<String, Object>();
    tokens.put("ERA", getText(gc.get(Calendar.ERA), "BC", "AD"));
    tokens.put("YEAR", getString(gc.get(Calendar.YEAR), 4, "0"));
    tokens.put("MONTH", getString(gc.get(Calendar.MONTH) + 1, 2, "0"));
    tokens.put("WEEK_OF_YEAR", getString(gc.get(Calendar.WEEK_OF_YEAR), 2, "0"));
    tokens.put("WEEK_OF_MONTH", getString(gc.get(Calendar.WEEK_OF_MONTH), 0, ""));
    tokens.put("DATE", getString(gc.get(Calendar.DATE), 2, "0"));
    tokens.put("DAY_OF_MONTH", getString(gc.get(Calendar.DAY_OF_MONTH), 2, "0"));
    tokens.put("DAY_OF_YEAR", getString(gc.get(Calendar.DAY_OF_YEAR), 3, "0"));
    tokens.put("DAY_OF_WEEK", getString(gc.get(Calendar.DAY_OF_WEEK), 0, ""));
    tokens.put("DAY_OF_WEEK_IN_MONTH", getString(gc.get(Calendar.DAY_OF_WEEK_IN_MONTH), 0, ""));
    tokens.put("AM_PM", getText(gc.get(Calendar.AM_PM), "AM", "PM"));
    tokens.put("HOUR", getString(gc.get(Calendar.HOUR), 2, "0"));
    tokens.put("HOUR_OF_DAY", getString(gc.get(Calendar.HOUR_OF_DAY), 2, "0"));
    tokens.put("MINUTE", getString(gc.get(Calendar.MINUTE), 2, "0"));
    tokens.put("SECOND", getString(gc.get(Calendar.SECOND), 2, "0"));
    tokens.put("MILLISECOND", getString(gc.get(Calendar.MILLISECOND), 3, "0"));
    // tokens.put("ZONE_OFFSET", getString((gc.get(Calendar.ZONE_OFFSET)/(60*60*1000)), 0, ""));
    // tokens.put("DST_OFFSET", getString((gc.get(Calendar.DST_OFFSET)/(60*60*1000)), 0, ""));
    tokens.put("COUNTER", getString(counter, 0, ""));

    return processParams(path, tokens);
}