Example usage for java.util Calendar DAY_OF_WEEK_IN_MONTH

List of usage examples for java.util Calendar DAY_OF_WEEK_IN_MONTH

Introduction

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

Prototype

int DAY_OF_WEEK_IN_MONTH

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

Click Source Link

Document

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

Usage

From source file:com.aimluck.eip.schedule.util.ScheduleUtils.java

/**
 * ?????????????//w  ww .  j a v a2  s  .c  om
 *
 * @param date
 * @param ptn
 * @param startDate
 * @param limitDate
 * @return
 */
public static boolean isView(ALDateTimeField date, String ptn, Date startDate, Date limitDate) {
    int count = 0;
    boolean result = false;
    Calendar cal = Calendar.getInstance();
    cal.setTime(date.getValue());
    // 
    if (ptn.charAt(0) == 'D') {
        result = true;
        count = 1;
        // , 
    } else if (ptn.charAt(0) == 'W') {

        int dow = cal.get(Calendar.DAY_OF_WEEK);
        // ??
        int dowim = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
        if (ptn.charAt(8) == 'N' || ptn.charAt(8) == 'L' || dowim == Character.getNumericValue(ptn.charAt(8))) {
            switch (dow) {
            // 
            case Calendar.SUNDAY:
                result = ptn.charAt(1) != '0';
                break;
            // 
            case Calendar.MONDAY:
                result = ptn.charAt(2) != '0';
                break;
            // ?
            case Calendar.TUESDAY:
                result = ptn.charAt(3) != '0';
                break;
            // 
            case Calendar.WEDNESDAY:
                result = ptn.charAt(4) != '0';
                break;
            // 
            case Calendar.THURSDAY:
                result = ptn.charAt(5) != '0';
                break;
            // 
            case Calendar.FRIDAY:
                result = ptn.charAt(6) != '0';
                break;
            // 
            case Calendar.SATURDAY:
                result = ptn.charAt(7) != '0';
                break;
            default:
                result = false;
                break;
            }
            if (ptn.length() == 9) {
                count = 8;
            } else {
                count = 9;
            }
        }
        // 
    } else if (ptn.charAt(0) == 'M') {
        int mday;
        if (ptn.substring(1, 3).equals("XX")) {
            mday = cal.getActualMaximum(Calendar.DATE);
        } else {
            mday = Integer.parseInt(ptn.substring(1, 3));
        }
        result = Integer.parseInt(date.getDay()) == mday;
        count = 3;
    } else if (ptn.charAt(0) == 'Y') {
        int ymonth = Integer.parseInt(ptn.substring(1, 3));
        int yday = Integer.parseInt(ptn.substring(3, 5));
        int month = Integer.parseInt(date.getMonth());
        int day = Integer.parseInt(date.getDay());
        if (ymonth == month && yday == day) {
            result = true;
            count = 5;
        } else {
            result = false;
        }
    } else {
        return true;
    }

    if (result) {
        if (ptn.charAt(count) == 'L') {
            // ????
            if (equalsToDate(startDate, date.getValue(), false)
                    || equalsToDate(limitDate, date.getValue(), false)) {
                // ???
                result = true;
            } else {
                // ????????????
                result = result && startDate.before(cal.getTime()) && limitDate.after(cal.getTime());
            }
        }
    }

    return result;
}

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   www.  j  ava2s.c o  m*/

    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.aimluck.eip.schedule.util.ScheduleUtils.java

public static boolean isDuplicateFacilitySchedule(EipTSchedule schedule, List<Integer> facilityIdList,
        Integer _old_scheduleid, Date _old_viewDate) {
    /*  *///from   w ww  .j a  v a 2s. com
    GregorianCalendar cald = new GregorianCalendar();

    boolean result = false;
    {

        Date start_date;
        Date end_date;
        String repeat_pattern;
        String repeat_type;
        String repeat_week = null;
        boolean week_0;
        boolean week_1;
        boolean week_2;
        boolean week_3;
        boolean week_4;
        boolean week_5;
        boolean week_6;
        boolean day_of_week_in_month_1;
        boolean day_of_week_in_month_2;
        boolean day_of_week_in_month_3;
        boolean day_of_week_in_month_4;
        boolean day_of_week_in_month_5;
        boolean[] day_of_week_in_month_array = new boolean[5];
        String limit_flag;
        int month_day = -1;
        int year_month = -1;
        int year_day = -1;
        Integer db_scheduleid = null;
        boolean[] week_array = new boolean[7];
        boolean unlimited_repeat = false;
        try {
            start_date = schedule.getStartDate();

            end_date = schedule.getEndDate();

            repeat_pattern = schedule.getRepeatPattern();

            repeat_type = repeat_pattern.substring(0, 1);

            day_of_week_in_month_1 = repeat_pattern.matches("W.......1.?");

            day_of_week_in_month_2 = repeat_pattern.matches("W.......2.?");

            day_of_week_in_month_3 = repeat_pattern.matches("W.......3.?");

            day_of_week_in_month_4 = repeat_pattern.matches("W.......4.?");

            day_of_week_in_month_5 = repeat_pattern.matches("W.......5.?");

            if (repeat_type.equals("W")) {
                if (repeat_pattern.length() == 9) {
                    repeat_week = "0";
                    day_of_week_in_month_1 = true;
                    day_of_week_in_month_2 = true;
                    day_of_week_in_month_3 = true;
                    day_of_week_in_month_4 = true;
                    day_of_week_in_month_5 = true;
                } else {
                    repeat_week = repeat_pattern.substring(8, 9);
                }
            }

            limit_flag = repeat_pattern.substring(repeat_pattern.length() - 1);

            week_0 = repeat_pattern.matches("W1........?");

            week_1 = repeat_pattern.matches("W.1.......?");

            week_2 = repeat_pattern.matches("W..1......?");

            week_3 = repeat_pattern.matches("W...1.....?");

            week_4 = repeat_pattern.matches("W....1....?");

            week_5 = repeat_pattern.matches("W.....1...?");

            week_6 = repeat_pattern.matches("W......1..?");

            if (repeat_pattern.startsWith("M")) {
                month_day = Integer.parseInt(repeat_pattern.substring(1, 3));
            }

            if (repeat_pattern.startsWith("Y")) {
                year_month = Integer.parseInt(repeat_pattern.substring(1, 3));
                year_day = Integer.parseInt(repeat_pattern.substring(3, 5));
            }
            // ???1??????
            if (repeat_pattern.startsWith("N")) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(start_date);
                int dow = cal.get(Calendar.DAY_OF_WEEK);
                week_0 = (dow == Calendar.SUNDAY);
                week_1 = (dow == Calendar.MONDAY);
                week_2 = (dow == Calendar.TUESDAY);
                week_3 = (dow == Calendar.WEDNESDAY);
                week_4 = (dow == Calendar.THURSDAY);
                week_5 = (dow == Calendar.FRIDAY);
                week_6 = (dow == Calendar.SATURDAY);
                month_day = cal.get(Calendar.DAY_OF_MONTH);
                int dowim = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
                day_of_week_in_month_1 = (dowim == 1);
                day_of_week_in_month_2 = (dowim == 2);
                day_of_week_in_month_3 = (dowim == 3);
                day_of_week_in_month_4 = (dowim == 4);
                day_of_week_in_month_5 = (dowim == 5);
                year_month = cal.get(Calendar.MONTH) + 1;
                year_day = cal.get(Calendar.DAY_OF_MONTH);
            } else if (repeat_pattern.endsWith("N")) {
                unlimited_repeat = true;
            }

            week_array[0] = week_0;
            week_array[1] = week_1;
            week_array[2] = week_2;
            week_array[3] = week_3;
            week_array[4] = week_4;
            week_array[5] = week_5;
            week_array[6] = week_6;

            day_of_week_in_month_array[0] = day_of_week_in_month_1;
            day_of_week_in_month_array[1] = day_of_week_in_month_2;
            day_of_week_in_month_array[2] = day_of_week_in_month_3;
            day_of_week_in_month_array[3] = day_of_week_in_month_4;
            day_of_week_in_month_array[4] = day_of_week_in_month_5;

        } catch (RuntimeException e) {
            logger.error("schedule", e);
            return false;
        } catch (Exception e) {
            logger.error("schedule", e);
            return false;
        }

        if (repeat_type.equals("S")) {
            // ??0:00:00?????23:59:59??
            Calendar cal = Calendar.getInstance();
            cal.setTime(end_date);
            cal.add(Calendar.DATE, 1);
            cal.add(Calendar.MINUTE, -1);
            end_date = cal.getTime();
        }

        // ???
        if (facilityIdList.size() > 0) {//
            List<Integer> fids = facilityIdList;
            SelectQuery<EipTScheduleMap> fquery = Database.query(EipTScheduleMap.class);
            Expression fexp1 = ExpressionFactory.inExp(EipTScheduleMap.USER_ID_PROPERTY, fids);
            fquery.setQualifier(fexp1);

            Expression fexp2 = ExpressionFactory.matchExp(EipTScheduleMap.TYPE_PROPERTY,
                    ScheduleUtils.SCHEDULEMAP_TYPE_FACILITY);
            fquery.andQualifier(fexp2);

            Expression oneexp = null;// 1
            Expression spanexp = null;// 
            Expression rdexp = null;// 
            Expression rwexp = null;// 
            Expression rwexp2 = null;
            // Expression rwlexp = null;
            Expression rmexp = null;// 
            Expression ryexp = null;

            { // ?
                Expression exp100 = ExpressionFactory.matchExp(
                        EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "." + EipTSchedule.REPEAT_PATTERN_PROPERTY,
                        "N");

                try {
                    if (!unlimited_repeat) {
                        Expression exp101 = ExpressionFactory.lessOrEqualExp(
                                EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "." + EipTSchedule.START_DATE_PROPERTY,
                                end_date);// EipTSchedule.START_DATE_PROPERTY <= end_date
                        Expression exp102 = ExpressionFactory.greaterExp(
                                EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "." + EipTSchedule.END_DATE_PROPERTY,
                                start_date);// EipTSchedule.END_DATE_PROPERTY > start_date

                        oneexp = exp100.andExp(exp101.andExp(exp102));

                    } else {
                        oneexp = exp100;
                    }
                } catch (Exception e) {

                }
            }

            { // ?
                Expression exp200 = ExpressionFactory.matchExp(
                        EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "." + EipTSchedule.REPEAT_PATTERN_PROPERTY,
                        "S");

                try {
                    if (!unlimited_repeat) {
                        // ??00:00??????
                        Calendar cal_end = Calendar.getInstance();
                        cal_end.setTime(end_date);
                        cal_end = DateUtils.truncate(cal_end, Calendar.DAY_OF_MONTH);
                        Expression exp201 = ExpressionFactory.lessOrEqualExp(
                                EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "." + EipTSchedule.START_DATE_PROPERTY,
                                cal_end.getTime());
                        // EipTSchedule.START_DATE_PROPERTY <= end_date
                        Calendar cal_start = Calendar.getInstance();
                        cal_start.setTime(start_date);
                        cal_start = DateUtils.truncate(cal_start, Calendar.DAY_OF_MONTH);
                        Expression exp202 = ExpressionFactory.greaterOrEqualExp(
                                EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "." + EipTSchedule.END_DATE_PROPERTY,
                                cal_start.getTime());
                        // EipTSchedule.END_DATE_PROPERTY >= start_date

                        spanexp = exp200.andExp(exp201.andExp(exp202));

                    } else {
                        spanexp = exp200;
                    }
                } catch (Exception e) {

                }
            }

            { // ??
              // char lim = 'N';
                if ("ON".equals(limit_flag)) {
                    // lim = 'L';
                }

                { // "D".equals(repeat_type.getValue())
                    Expression dexp01 = ExpressionFactory.likeExp(
                            EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "." + EipTSchedule.REPEAT_PATTERN_PROPERTY,
                            "D_");
                    rdexp = dexp01;
                }

                { // "W".equals(repeat_type.getValue())
                    Expression wexp = null;
                    List<Expression> wexps = new ArrayList<Expression>();
                    if (week_0 == true) {
                        wexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W1_______");
                        wexps.add(wexp);
                    }
                    if (week_1 == true) {
                        wexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_1______");
                        wexps.add(wexp);
                    }
                    if (week_2 == true) {
                        wexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W__1_____");
                        wexps.add(wexp);
                    }
                    if (week_3 == true) {
                        wexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W___1____");
                        wexps.add(wexp);
                    }
                    if (week_4 == true) {
                        wexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W____1___");
                        wexps.add(wexp);
                    }
                    if (week_5 == true) {
                        wexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_____1__");
                        wexps.add(wexp);
                    }
                    if (week_6 == true) {
                        wexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W______1_");
                        wexps.add(wexp);
                    }
                    if (wexps.size() > 0) {
                        rwexp = wexps.get(0);
                        int wexpssize = wexps.size();
                        for (int k = 1; k < wexpssize; k++) {
                            rwexp = rwexp.orExp(wexps.get(k));
                        }
                    } else {
                        rwexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W________");
                    }
                }
                {
                    Expression wexp2 = null;
                    List<Expression> wexps2 = new ArrayList<Expression>();
                    Expression wnexp = null;
                    List<Expression> wnexp2 = new ArrayList<Expression>();

                    if (week_0 == true) {
                        wexp2 = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W1________");
                        wexps2.add(wexp2);
                    }
                    if (week_1 == true) {
                        wexp2 = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_1_______");
                        wexps2.add(wexp2);
                    }
                    if (week_2 == true) {
                        wexp2 = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W__1______");
                        wexps2.add(wexp2);
                    }
                    if (week_3 == true) {
                        wexp2 = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W___1_____");
                        wexps2.add(wexp2);
                    }
                    if (week_4 == true) {
                        wexp2 = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W____1____");
                        wexps2.add(wexp2);
                    }
                    if (week_5 == true) {
                        wexp2 = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_____1___");
                        wexps2.add(wexp2);
                    }
                    if (week_6 == true) {
                        wexp2 = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W______1__");
                        wexps2.add(wexp2);
                    }
                    if (repeat_week != null && repeat_week.equals("1") || day_of_week_in_month_1) {
                        wnexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_______1_");
                        wnexp2.add(wnexp);
                    }
                    if (repeat_week != null && repeat_week.equals("2") || day_of_week_in_month_2) {
                        wnexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_______2_");
                        wnexp2.add(wnexp);
                    }
                    if (repeat_week != null && repeat_week.equals("3") || day_of_week_in_month_3) {
                        wnexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_______3_");
                        wnexp2.add(wnexp);
                    }
                    if (repeat_week != null && repeat_week.equals("4") || day_of_week_in_month_4) {
                        wnexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_______4_");
                        wnexp2.add(wnexp);
                    }
                    if (repeat_week != null && repeat_week.equals("5") || day_of_week_in_month_5) {
                        wnexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_______5_");
                        wnexp2.add(wnexp);
                    }
                    if (wexps2.size() > 0 && wnexp2.size() > 0) {
                        for (int k = 0; k < wexps2.size(); k++) {
                            for (int l = 0; l < wnexp2.size(); l++) {
                                if (k == 0 && l == 0) {
                                    rwexp2 = wexps2.get(k).andExp(wnexp2.get(l));
                                } else {
                                    rwexp2 = rwexp2.orExp(wexps2.get(k).andExp(wnexp2.get(l)));
                                }
                            }
                        }
                    } else {
                        rwexp2 = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "W_________");
                    }

                }

                { // "M".equals(repeat_type.getValue())
                    if (month_day > 0) { // ????????
                        DecimalFormat exF = new DecimalFormat("00");
                        String md_str = exF.format(month_day);
                        rmexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "M" + md_str + "_");

                    } else if (year_day > 0 && year_month > 0) { // ??
                        DecimalFormat exG = new DecimalFormat("00");
                        String yd_str = exG.format(year_day);
                        rmexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "M" + yd_str + "_");
                    } else {
                        rmexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "M___");
                    }
                }

                { // "Y".equals(repeat_type.getValue())
                    if (year_day > 0 && year_month > 0) { // ????????
                        DecimalFormat exG = new DecimalFormat("00");
                        String ym_str = exG.format(year_month);
                        String yd_str = exG.format(year_day);

                        ryexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "Y" + ym_str + yd_str + "_");
                    } else if (month_day > 0) { // ??
                        DecimalFormat exF = new DecimalFormat("00");
                        String md_str = exF.format(month_day);
                        ryexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "Y__" + md_str + "_");
                    } else {
                        ryexp = ExpressionFactory.likeExp(EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "."
                                + EipTSchedule.REPEAT_PATTERN_PROPERTY, "Y_____");
                    }
                }

                Expression repeatexp = oneexp;
                if (rdexp != null) {
                    repeatexp = repeatexp.orExp(rdexp);
                }
                if (rwexp != null) {
                    repeatexp = repeatexp.orExp(rwexp);
                }
                if (rwexp2 != null) {
                    repeatexp = repeatexp.orExp(rwexp2);
                }
                if (rmexp != null) {
                    repeatexp = repeatexp.orExp(rmexp);
                }
                if (spanexp != null) {
                    repeatexp = repeatexp.orExp(spanexp);
                }
                if (ryexp != null) {
                    repeatexp = repeatexp.orExp(ryexp);
                }
                fquery.andQualifier(repeatexp);
            }

            db_scheduleid = schedule.getScheduleId();
            if (db_scheduleid != null && db_scheduleid >= 0) {
                Expression exp00 = ExpressionFactory.noMatchDbExp(
                        EipTScheduleMap.EIP_TSCHEDULE_PROPERTY + "." + EipTSchedule.SCHEDULE_ID_PK_COLUMN,
                        db_scheduleid);
                fquery.andQualifier(exp00);
            }

            fquery.distinct(true);
            List<EipTScheduleMap> f_list = fquery.fetchList();
            if (f_list != null && f_list.size() > 0) {
                // ?????
                boolean existFacility = false;
                int f_list_size = f_list.size();
                for (int i = 0; i < f_list_size; i++) {
                    EipTScheduleMap map = f_list.get(i);

                    Date dbStartDate = map.getEipTSchedule().getStartDate();
                    Date dbEndDate = map.getEipTSchedule().getEndDate();

                    boolean containtsRs = false;
                    // ??????
                    String ptn = map.getEipTSchedule().getRepeatPattern();
                    if (ptn.charAt(0) == 'S') { // 
                        try {
                            // ??0:00:00?????23:59:59??
                            Calendar cal = Calendar.getInstance();
                            cal.setTime(dbEndDate);
                            cal.add(Calendar.DATE, 1);
                            cal.add(Calendar.MINUTE, -1);
                            dbEndDate = cal.getTime();

                            if ((end_date.after(dbStartDate) && start_date.before(dbEndDate))
                                    || unlimited_repeat) {
                                containtsRs = true;
                            }
                        } catch (Exception e) {
                            containtsRs = false;
                        }
                    } else if (ptn.charAt(0) == 'N') { // ?
                        if ("D".equals(repeat_type) || "N".equals(repeat_type)) { //  or
                            // ?
                            try {
                                if ((dbStartDate.before(end_date) && dbEndDate.after(start_date))
                                        || unlimited_repeat) {
                                    containtsRs = true;
                                }
                            } catch (Exception e) {
                                containtsRs = false;
                            }
                        } else {
                            if ((dbStartDate.before(end_date) && dbEndDate.after(start_date))
                                    || unlimited_repeat) {
                                containtsRs = true;
                            }
                        }
                    } else if (ptn.charAt(0) == 'D') {// 
                        if (ptn.charAt(1) == 'L') {
                            try {
                                if ((dbStartDate.before(end_date) && dbEndDate.after(start_date))
                                        || unlimited_repeat) {
                                    containtsRs = true;
                                }
                            } catch (Exception e) {
                                containtsRs = false;
                            }
                        } else {
                            containtsRs = true;
                        }
                    } else if (ptn.charAt(0) == 'W') {
                        if (ptn.length() == 9) {
                            if (ptn.charAt(8) == 'L') {
                                try {
                                    if ((dbStartDate.before(end_date) && dbEndDate.after(start_date))
                                            || unlimited_repeat) {
                                        containtsRs = true;
                                    }
                                } catch (Exception e) {
                                    containtsRs = false;
                                }
                            } else {
                                containtsRs = true;
                            }
                        } else if (ptn.length() == 10) {
                            if (ptn.charAt(9) == 'L') {
                                try {
                                    if ((dbStartDate.before(end_date) && dbEndDate.after(start_date))
                                            || unlimited_repeat) {
                                        containtsRs = true;
                                    }
                                } catch (Exception e) {
                                    containtsRs = false;
                                }
                            } else {
                                containtsRs = true;
                            }
                        }
                    } else if (ptn.charAt(0) == 'M') {
                        if (ptn.charAt(3) == 'L') {
                            try {
                                if ((dbStartDate.before(end_date) && dbEndDate.after(start_date))
                                        || unlimited_repeat) {
                                    containtsRs = true;
                                }
                            } catch (Exception e) {
                                containtsRs = false;
                            }
                        } else {
                            containtsRs = true;
                        }
                    } else if (ptn.charAt(0) == 'Y') {
                        if (ptn.charAt(5) == 'L') {
                            try {
                                if ((dbStartDate.before(end_date) && dbEndDate.after(start_date))
                                        || unlimited_repeat) {
                                    containtsRs = true;
                                }
                            } catch (Exception e) {
                                containtsRs = false;
                            }
                        } else {
                            containtsRs = true;
                        }
                    } else {
                        containtsRs = true;
                    }

                    if (containtsRs) {
                        /* ?????? */
                        int ss_flg = ScheduleUtils.compareTime(start_date, dbEndDate);
                        int se_flg = ScheduleUtils.compareTime(end_date, dbStartDate);
                        if (ss_flg > 0 && se_flg < 0) {
                            /* ???????????? */
                            if (!"N".equals(ptn) && ptn.endsWith("N") && unlimited_repeat) {// ?(?????&&???)&&????
                                existFacility = true;
                            } else {
                                Date _start_date = null;
                                Date _end_date = null;

                                if (!"N".equals(ptn) && ptn.endsWith("N") && !unlimited_repeat) {// ?(??&&???)&&???
                                    _start_date = (Date) start_date.clone();
                                    _end_date = (Date) end_date.clone();
                                } else if (("N".equals(ptn) || !ptn.endsWith("N")) && unlimited_repeat) {// ?(?||??)&&????
                                    _start_date = (Date) dbStartDate.clone();
                                    _end_date = (Date) dbEndDate.clone();
                                } else if (("N".equals(ptn) || !ptn.endsWith("N")) && !unlimited_repeat) {// ?(?||??)&&???

                                    if (dbStartDate.after(start_date)) {
                                        _start_date = (Date) dbStartDate.clone();
                                    } else {
                                        _start_date = (Date) start_date.clone();
                                    }

                                    if (dbEndDate.before(end_date)) {
                                        _end_date = (Date) dbEndDate.clone();
                                    } else {
                                        _end_date = (Date) end_date.clone();
                                    }

                                }

                                if ((_start_date == null) || (_end_date == null)) {
                                    continue;
                                }

                                /* ??? */
                                Expression dexp1 = ExpressionFactory.matchExp(EipTSchedule.NAME_PROPERTY,
                                        "dummy");// 

                                Expression dexp2 = ExpressionFactory.matchExp(EipTSchedule.PARENT_ID_PROPERTY,
                                        map.getScheduleId());

                                if (db_scheduleid != null) {
                                    Expression dexp21 = ExpressionFactory
                                            .matchExp(EipTSchedule.PARENT_ID_PROPERTY, db_scheduleid);
                                    dexp2 = dexp2.orExp(dexp21);
                                }
                                Expression dexp3 = null;

                                cald.setTime(_start_date);
                                cald.set(Calendar.MILLISECOND, 0);
                                cald.set(Calendar.SECOND, 0);
                                cald.set(Calendar.MINUTE, 0);
                                cald.set(Calendar.HOUR_OF_DAY, 0);
                                Date ddate = cald.getTime();// _start_date?
                                List<EipTSchedule> temp = null;

                                if ("N".equals(repeat_pattern)) {
                                    /* ?????????????? */
                                    if ((_old_scheduleid != null) && (_old_viewDate != null)) {
                                        if ((_old_scheduleid.intValue() == map.getScheduleId().intValue())
                                                && compareToDate(_start_date, _old_viewDate) == 0) {
                                            continue;
                                        }
                                    }

                                    try {
                                        dexp3 = ExpressionFactory.matchExp(EipTSchedule.START_DATE_PROPERTY,
                                                ddate);
                                        temp = Database
                                                .query(EipTSchedule.class, dexp1.andExp(dexp2).andExp(dexp3))
                                                .fetchList();
                                        if (temp == null || temp.size() <= 0) {
                                            existFacility = true;
                                            break;
                                        }
                                    } catch (Exception e) {
                                        logger.error("[DuplicateFacilityCheck]: ", e);
                                        existFacility = true;
                                        break;
                                    }
                                } else if (repeat_pattern.startsWith("D")) {
                                    while (!ddate.after(_end_date)) {
                                        if (matchDay(cald, ptn)) {
                                            try {
                                                dexp3 = ExpressionFactory
                                                        .matchExp(EipTSchedule.START_DATE_PROPERTY, ddate);
                                                temp = Database.query(EipTSchedule.class,
                                                        dexp1.andExp(dexp2).andExp(dexp3)).fetchList();
                                                if (temp == null || temp.size() <= 0) {
                                                    existFacility = true;
                                                    break;
                                                }
                                            } catch (Exception e) {
                                                logger.error("[DuplicateFacilityCheck]: ", e);
                                                existFacility = true;
                                                break;
                                            }
                                        }
                                        cald.add(Calendar.DATE, 1);
                                        ddate = cald.getTime();
                                    }
                                } else if (repeat_pattern.startsWith("S")) {
                                    while (!ddate.after(_end_date)) {
                                        try {
                                            dexp3 = ExpressionFactory.matchExp(EipTSchedule.START_DATE_PROPERTY,
                                                    ddate);
                                            temp = Database.query(EipTSchedule.class,
                                                    dexp1.andExp(dexp2).andExp(dexp3)).fetchList();
                                            if (temp == null || temp.size() <= 0) {
                                                existFacility = true;
                                                break;
                                            }
                                        } catch (Exception e) {
                                            logger.error("[DuplicateFacilityCheck]: ", e);
                                            existFacility = true;
                                            break;
                                        }
                                        cald.add(Calendar.DATE, 1);
                                        ddate = cald.getTime();
                                    }
                                } else if (repeat_pattern.startsWith("W")) {
                                    /* ? */
                                    int wlen = week_array.length;
                                    int wlen2 = day_of_week_in_month_array.length;
                                    if (wlen < 1 || wlen2 < 1) {
                                        continue;
                                    }
                                    int k;
                                    int l;
                                    while (!ddate.after(_end_date)) {
                                        k = (cald.get(Calendar.DAY_OF_WEEK) - 1) % wlen;
                                        l = (cald.get(Calendar.DAY_OF_WEEK_IN_MONTH) - 1) % wlen2;
                                        if ((week_array[k] == true) && (day_of_week_in_month_array[l] == true)
                                                && matchDay(cald, ptn)) {
                                            try {
                                                dexp3 = ExpressionFactory
                                                        .matchExp(EipTSchedule.START_DATE_PROPERTY, ddate);
                                                temp = Database.query(EipTSchedule.class,
                                                        dexp1.andExp(dexp2).andExp(dexp3)).fetchList();// SQL
                                                if (temp == null || temp.size() <= 0) {
                                                    existFacility = true;// true????
                                                    break;
                                                }
                                            } catch (Exception e) {
                                                logger.error("[DuplicateFacilityCheck]: ", e);
                                                existFacility = true;
                                                break;
                                            }
                                        }
                                        cald.add(Calendar.DATE, 1);
                                        ddate = cald.getTime();
                                    }
                                } else if (repeat_pattern.startsWith("M")) {
                                    /* ?? */
                                    cald.setTime(dbStartDate);
                                    cald.set(Calendar.MILLISECOND, 0);
                                    cald.set(Calendar.SECOND, 0);
                                    cald.set(Calendar.MINUTE, 0);
                                    cald.set(Calendar.HOUR_OF_DAY, 0);

                                    if (month_day > 0) {
                                        cald.set(Calendar.DAY_OF_MONTH, month_day);
                                    } else {
                                        continue;
                                    }
                                    Date tmp_date = cald.getTime();
                                    while (tmp_date.before(ddate)) {
                                        cald.add(Calendar.MONTH, 1);
                                        /* ???????????????? */
                                        while (month_day > cald.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                                            cald.add(Calendar.MONTH, 1);
                                            cald.set(Calendar.DAY_OF_MONTH, month_day);
                                            if (tmp_date.before(tmp_date)) {
                                                break;
                                            }
                                        }
                                        tmp_date = cald.getTime();
                                    }
                                    ddate = tmp_date;
                                    /*  */
                                    while (!ddate.after(_end_date)) {
                                        if (matchDay(cald, ptn)) {
                                            try {
                                                dexp3 = ExpressionFactory
                                                        .matchExp(EipTSchedule.START_DATE_PROPERTY, ddate);
                                                temp = Database.query(EipTSchedule.class,
                                                        dexp1.andExp(dexp2).andExp(dexp3)).fetchList();
                                                if (temp == null || temp.size() <= 0) {
                                                    existFacility = true;
                                                    break;
                                                }

                                            } catch (Exception e) {
                                                logger.error("[DuplicateFacilityCheck]: ", e);
                                                existFacility = true;
                                                break;
                                            }
                                        }
                                        cald.add(Calendar.MONTH, 1);
                                        /* ???????????????? */
                                        while (month_day > cald.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                                            cald.add(Calendar.MONTH, 1);
                                            cald.set(Calendar.DAY_OF_MONTH, month_day);
                                            if (!ddate.after(_end_date)) {
                                                break;
                                            }
                                        }
                                        ddate = cald.getTime();
                                    }
                                } else if (repeat_pattern.startsWith("Y")) {
                                    /* ?? */
                                    cald.setTime(dbStartDate);
                                    cald.set(Calendar.MILLISECOND, 0);
                                    cald.set(Calendar.SECOND, 0);
                                    cald.set(Calendar.MINUTE, 0);
                                    cald.set(Calendar.HOUR_OF_DAY, 0);

                                    if (year_month > 0 && year_day > 0) {
                                        cald.set(Calendar.MONTH, year_month - 1);
                                        cald.set(Calendar.DAY_OF_MONTH, year_day);
                                    } else {
                                        continue;
                                    }
                                    Date tmp_date = cald.getTime();
                                    while (tmp_date.before(ddate)) {
                                        cald.add(Calendar.MONTH, 1);
                                        /* ???????????????? */
                                        while (year_day > cald.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                                            cald.add(Calendar.MONTH, 1);
                                            cald.set(Calendar.DAY_OF_MONTH, year_day);
                                            if (tmp_date.before(tmp_date)) {
                                                break;
                                            }
                                        }
                                        tmp_date = cald.getTime();
                                    }
                                    ddate = tmp_date;
                                    /*  */
                                    while (!ddate.after(_end_date)) {
                                        if (matchDay(cald, ptn)) {
                                            try {
                                                dexp3 = ExpressionFactory
                                                        .matchExp(EipTSchedule.START_DATE_PROPERTY, ddate);
                                                temp = Database.query(EipTSchedule.class,
                                                        dexp1.andExp(dexp2).andExp(dexp3)).fetchList();
                                                if (temp == null || temp.size() <= 0) {
                                                    existFacility = true;
                                                    break;
                                                }
                                            } catch (Exception e) {
                                                logger.error("[DuplicateFacilityCheck]: ", e);
                                                existFacility = true;
                                                break;
                                            }
                                        }
                                        cald.add(Calendar.MONTH, 1);
                                        /* ???????????????? */
                                        while (year_day > cald.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                                            cald.add(Calendar.MONTH, 1);
                                            cald.set(Calendar.DAY_OF_MONTH, year_day);
                                            if (!ddate.after(_end_date)) {
                                                break;
                                            }
                                        }
                                        ddate = cald.getTime();
                                    }
                                } else {
                                    continue;
                                }
                            }
                        }
                    }
                    if (existFacility) {
                        break;
                    }
                }
                if (existFacility) {
                    return existFacility;
                }
            }
        }
    }
    return result;
}

From source file:com.aimluck.eip.schedule.util.ScheduleUtils.java

public static boolean matchDay(Calendar cal, String repeat_ptn) { // ??????????
    if (repeat_ptn == null || "".equals(repeat_ptn)) {
        return false;
    }/*from w w  w  .  jav a 2  s.co  m*/
    if (repeat_ptn.startsWith("M")) {
        int month_day;
        // ?
        if (repeat_ptn.substring(1, 3).equals("XX")) {
            month_day = cal.getActualMaximum(Calendar.DATE);
        } else {
            month_day = Integer.parseInt(repeat_ptn.substring(1, 3));
        }
        int ptn_day = cal.get(Calendar.DAY_OF_MONTH);
        return (month_day == ptn_day);
    }
    if (repeat_ptn.startsWith("Y")) {
        int year_month = Integer.parseInt(repeat_ptn.substring(1, 3));
        int year_day = Integer.parseInt(repeat_ptn.substring(3, 5));
        int ptn_month = cal.get(Calendar.MONTH);
        int ptn_day = cal.get(Calendar.DAY_OF_MONTH);
        return (year_day == ptn_day && (year_month - 1) == ptn_month);
    } else if (repeat_ptn.startsWith("W")) {
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        int dowim = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
        if (repeat_ptn.length() == 9 || dowim == Character.getNumericValue(repeat_ptn.charAt(8))) {
            if (dow == Calendar.SUNDAY) {
                return repeat_ptn.matches("W1........?");
            }
            if (dow == Calendar.MONDAY) {
                return repeat_ptn.matches("W.1.......?");
            }
            if (dow == Calendar.TUESDAY) {
                return repeat_ptn.matches("W..1......?");
            }
            if (dow == Calendar.WEDNESDAY) {
                return repeat_ptn.matches("W...1.....?");
            }
            if (dow == Calendar.THURSDAY) {
                return repeat_ptn.matches("W....1....?");
            }
            if (dow == Calendar.FRIDAY) {
                return repeat_ptn.matches("W.....1...?");
            }
            if (dow == Calendar.SATURDAY) {
                return repeat_ptn.matches("W......1..?");
            }
        }
        return false;
    } else {
        return true;
    }
}

From source file:org.alfresco.service.cmr.calendar.CalendarRecurrenceHelper.java

/**
 * Takes you to eg the 2nd Thursday in the month, which may
 *  involve going back before the current date  
 *///from  w  ww.ja  va 2  s  .  co  m
private static void toDayOfWeekInMonth(Calendar c, int dayOfWeek, int weekInMonth) {
    //set it to the first day
    c.set(Calendar.DATE, 1);
    //move to the day we need
    c.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    //and then to the month
    c.set(Calendar.DAY_OF_WEEK_IN_MONTH, weekInMonth);
}

From source file:org.apache.logging.log4j.core.util.datetime.FastDatePrinter.java

/**
 * <p>Returns a list of Rules given a pattern.</p>
 *
 * @return a {@code List} of Rule objects
 * @throws IllegalArgumentException if pattern is invalid
 *///  ww w  .  ja va2 s .  c o m
protected List<Rule> parsePattern() {
    final DateFormatSymbols symbols = new DateFormatSymbols(mLocale);
    final List<Rule> rules = new ArrayList<>();

    final String[] ERAs = symbols.getEras();
    final String[] months = symbols.getMonths();
    final String[] shortMonths = symbols.getShortMonths();
    final String[] weekdays = symbols.getWeekdays();
    final String[] shortWeekdays = symbols.getShortWeekdays();
    final String[] AmPmStrings = symbols.getAmPmStrings();

    final int length = mPattern.length();
    final int[] indexRef = new int[1];

    for (int i = 0; i < length; i++) {
        indexRef[0] = i;
        final String token = parseToken(mPattern, indexRef);
        i = indexRef[0];

        final int tokenLen = token.length();
        if (tokenLen == 0) {
            break;
        }

        Rule rule;
        final char c = token.charAt(0);

        switch (c) {
        case 'G': // era designator (text)
            rule = new TextField(Calendar.ERA, ERAs);
            break;
        case 'y': // year (number)
        case 'Y': // week year
            if (tokenLen == 2) {
                rule = TwoDigitYearField.INSTANCE;
            } else {
                rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen);
            }
            if (c == 'Y') {
                rule = new WeekYear((NumberRule) rule);
            }
            break;
        case 'M': // month in year (text and number)
            if (tokenLen >= 4) {
                rule = new TextField(Calendar.MONTH, months);
            } else if (tokenLen == 3) {
                rule = new TextField(Calendar.MONTH, shortMonths);
            } else if (tokenLen == 2) {
                rule = TwoDigitMonthField.INSTANCE;
            } else {
                rule = UnpaddedMonthField.INSTANCE;
            }
            break;
        case 'd': // day in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen);
            break;
        case 'h': // hour in am/pm (number, 1..12)
            rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen));
            break;
        case 'H': // hour in day (number, 0..23)
            rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen);
            break;
        case 'm': // minute in hour (number)
            rule = selectNumberRule(Calendar.MINUTE, tokenLen);
            break;
        case 's': // second in minute (number)
            rule = selectNumberRule(Calendar.SECOND, tokenLen);
            break;
        case 'S': // millisecond (number)
            rule = selectNumberRule(Calendar.MILLISECOND, tokenLen);
            break;
        case 'E': // day in week (text)
            rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays);
            break;
        case 'u': // day in week (number)
            rule = new DayInWeekField(selectNumberRule(Calendar.DAY_OF_WEEK, tokenLen));
            break;
        case 'D': // day in year (number)
            rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen);
            break;
        case 'F': // day of week in month (number)
            rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen);
            break;
        case 'w': // week in year (number)
            rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen);
            break;
        case 'W': // week in month (number)
            rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen);
            break;
        case 'a': // am/pm marker (text)
            rule = new TextField(Calendar.AM_PM, AmPmStrings);
            break;
        case 'k': // hour in day (1..24)
            rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen));
            break;
        case 'K': // hour in am/pm (0..11)
            rule = selectNumberRule(Calendar.HOUR, tokenLen);
            break;
        case 'X': // ISO 8601 
            rule = Iso8601_Rule.getRule(tokenLen);
            break;
        case 'z': // time zone (text)
            if (tokenLen >= 4) {
                rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG);
            } else {
                rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.SHORT);
            }
            break;
        case 'Z': // time zone (value)
            if (tokenLen == 1) {
                rule = TimeZoneNumberRule.INSTANCE_NO_COLON;
            } else if (tokenLen == 2) {
                rule = Iso8601_Rule.ISO8601_HOURS_COLON_MINUTES;
            } else {
                rule = TimeZoneNumberRule.INSTANCE_COLON;
            }
            break;
        case '\'': // literal text
            final String sub = token.substring(1);
            if (sub.length() == 1) {
                rule = new CharacterLiteral(sub.charAt(0));
            } else {
                rule = new StringLiteral(sub);
            }
            break;
        default:
            throw new IllegalArgumentException("Illegal pattern component: " + token);
        }

        rules.add(rule);
    }

    return rules;
}

From source file:org.callimachusproject.sql.SqlTupleResult.java

private Value value(int col) throws SQLException {
    int type = md.getColumnType(col);
    String str = rs.getString(col);
    if (str == null)
        return null;
    switch (type) {
    case java.sql.Types.NULL:
        return null;
    case java.sql.Types.DATALINK:
        return vf.createURI(str);
    case java.sql.Types.BINARY:
    case java.sql.Types.VARBINARY:
    case java.sql.Types.BIT:
    case java.sql.Types.BLOB:
    case java.sql.Types.LONGVARBINARY:
    case java.sql.Types.JAVA_OBJECT:
        return vf.createLiteral(Hex.encodeHexString(rs.getBytes(col)), XMLSchema.HEXBINARY);
    case java.sql.Types.DECIMAL:
    case java.sql.Types.NUMERIC:
        return vf.createLiteral(str, XMLSchema.DECIMAL);
    case java.sql.Types.TINYINT:
    case java.sql.Types.SMALLINT:
    case java.sql.Types.INTEGER:
    case java.sql.Types.BIGINT:
        return vf.createLiteral(str, XMLSchema.INTEGER);
    case java.sql.Types.DOUBLE:
    case java.sql.Types.FLOAT:
    case java.sql.Types.REAL:
        return vf.createLiteral(str, XMLSchema.DOUBLE);
    case java.sql.Types.BOOLEAN:
        return vf.createLiteral(rs.getBoolean(col));
    case java.sql.Types.DATE:
        GregorianCalendar date = new GregorianCalendar();
        date.setTime(rs.getDate(col));/*w  w  w. java  2s  . co m*/
        date.clear(Calendar.AM_PM);
        date.clear(Calendar.HOUR);
        date.clear(Calendar.HOUR_OF_DAY);
        date.clear(Calendar.MINUTE);
        date.clear(Calendar.SECOND);
        date.clear(Calendar.MILLISECOND);
        return vf.createLiteral(df.newXMLGregorianCalendar(date));
    case java.sql.Types.TIME:
        GregorianCalendar time = new GregorianCalendar();
        time.setTime(rs.getTime(col));
        time.clear(Calendar.ERA);
        time.clear(Calendar.YEAR);
        time.clear(Calendar.MONTH);
        time.clear(Calendar.WEEK_OF_YEAR);
        time.clear(Calendar.WEEK_OF_MONTH);
        time.clear(Calendar.DATE);
        time.clear(Calendar.DAY_OF_MONTH);
        time.clear(Calendar.DAY_OF_YEAR);
        time.clear(Calendar.DAY_OF_WEEK);
        time.clear(Calendar.DAY_OF_WEEK_IN_MONTH);
        return vf.createLiteral(df.newXMLGregorianCalendar(time));
    case java.sql.Types.TIMESTAMP:
        return vf.createLiteral(rs.getTimestamp(col));
    case java.sql.Types.SQLXML:
        return vf.createLiteral(str, RDF.XMLLITERAL);
    case java.sql.Types.ARRAY:
    case java.sql.Types.CHAR:
    case java.sql.Types.CLOB:
    case java.sql.Types.DISTINCT:
    case java.sql.Types.LONGNVARCHAR:
    case java.sql.Types.NCHAR:
    case java.sql.Types.NCLOB:
    case java.sql.Types.NVARCHAR:
    case java.sql.Types.OTHER:
    case java.sql.Types.REF:
    case java.sql.Types.ROWID:
    case java.sql.Types.STRUCT:
    case java.sql.Types.VARCHAR:
    default:
        return vf.createLiteral(str);
    }
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.models.spinner.SpinnerModelPropertyEditor.java

/**
 * @return the the name of step from {@link Calendar} fields.
 */// w w w  . j av a2  s .  c om
private static String getDateStep(int calendarField) {
    switch (calendarField) {
    case Calendar.ERA:
        return "ERA";
    case Calendar.YEAR:
        return "YEAR";
    case Calendar.MONTH:
        return "MONTH";
    case Calendar.WEEK_OF_YEAR:
        return "WEEK_OF_YEAR";
    case Calendar.WEEK_OF_MONTH:
        return "WEEK_OF_MONTH";
    case Calendar.DAY_OF_MONTH:
        return "DAY_OF_MONTH";
    case Calendar.DAY_OF_YEAR:
        return "DAY_OF_YEAR";
    case Calendar.DAY_OF_WEEK:
        return "DAY_OF_WEEK";
    case Calendar.DAY_OF_WEEK_IN_MONTH:
        return "DAY_OF_WEEK_IN_MONTH";
    case Calendar.AM_PM:
        return "AM_PM";
    case Calendar.HOUR:
        return "HOUR";
    case Calendar.HOUR_OF_DAY:
        return "HOUR_OF_DAY";
    case Calendar.MINUTE:
        return "MINUTE";
    case Calendar.SECOND:
        return "SECOND";
    case Calendar.MILLISECOND:
        return "MILLISECOND";
    default:
        return null;
    }
}

From source file:org.kalypso.commons.time.PeriodUtils.java

public static Period getPeriod(final int calendarField, final int amount) {
    switch (calendarField) {
    case Calendar.YEAR:
        return Period.years(amount);

    case Calendar.MONTH:
        return Period.months(amount);

    case Calendar.WEEK_OF_YEAR:
    case Calendar.WEEK_OF_MONTH:
        return Period.weeks(amount);

    case Calendar.DAY_OF_MONTH:
    case Calendar.DAY_OF_YEAR:
    case Calendar.DAY_OF_WEEK:
    case Calendar.DAY_OF_WEEK_IN_MONTH:
        return Period.days(amount);

    case Calendar.HOUR:
    case Calendar.HOUR_OF_DAY:
        return Period.hours(amount);

    case Calendar.MINUTE:
        return Period.minutes(amount);

    case Calendar.SECOND:
        return Period.seconds(amount);

    case Calendar.MILLISECOND:
        return Period.millis(amount);

    case Calendar.AM_PM:
    case Calendar.ERA:
    default://from  ww  w .jav a  2 s  .co  m
        throw new UnsupportedOperationException();
    }
}

From source file:org.mifos.calendar.CalendarUtils.java

public static DateTime getFirstDayForMonthUsingWeekRankAndWeekday(final DateTime startDate,
        final int calendarWeekOfMonth, final int dayOfWeek) {

    /*/*from w  ww  .  j a v a2  s .  c o m*/
     * In Joda time MONDAY=1 and SUNDAY=7, so shift these to SUNDAY=1, SATURDAY=7 to match this class
     */
    int calendarDayOfWeek = (dayOfWeek % 7) + 1;

    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate.toDate());

    DateTime scheduleDate = null;
    // if current weekday is after the weekday on which schedule has to
    // lie, move to next week
    if (gc.get(Calendar.DAY_OF_WEEK) > calendarDayOfWeek) {
        gc.add(Calendar.WEEK_OF_MONTH, 1);
    }
    // set the weekday on which schedule has to lie
    gc.set(Calendar.DAY_OF_WEEK, calendarDayOfWeek);
    // if week rank is First, Second, Third or Fourth, Set the
    // respective week.
    // if current week rank is after the weekrank on which schedule has
    // to lie, move to next month
    if (!RankOfDay.getRankOfDay(calendarWeekOfMonth).equals(RankOfDay.LAST)) {
        if (gc.get(Calendar.DAY_OF_WEEK_IN_MONTH) > calendarWeekOfMonth) {
            gc.add(GregorianCalendar.MONTH, 1);
            gc.set(GregorianCalendar.DATE, 1);
        }
        // set the weekrank on which schedule has to lie
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, calendarWeekOfMonth);
        scheduleDate = new DateTime(gc.getTime().getTime());
    } else {// scheduleData.getWeekRank()=Last
        int M1 = gc.get(GregorianCalendar.MONTH);
        // assumption: there are 5 weekdays in the month
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 5);
        int M2 = gc.get(GregorianCalendar.MONTH);
        // if assumption fails, it means there exists 4 weekdays in a
        // month, return last weekday date
        // if M1==M2, means there exists 5 weekdays otherwise 4 weekdays
        // in a month
        if (M1 != M2) {
            gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) - 1);
            gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 4);
        }
        scheduleDate = new DateTime(gc.getTime().getTime());
    }

    return scheduleDate;
}