Search in sources :

Example 66 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class RecurrenceRule method validByRule.

// Checks to see if a date is valid by the byXXX rules
private boolean validByRule(Date date) {
    // Build a Calendar object
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    // Test each byXXX rule.
    if (UtilValidate.isNotEmpty(bySecondList)) {
        if (!bySecondList.contains(String.valueOf(cal.get(Calendar.SECOND)))) {
            return false;
        }
    }
    if (UtilValidate.isNotEmpty(byMinuteList)) {
        if (!byMinuteList.contains(String.valueOf(cal.get(Calendar.MINUTE)))) {
            return false;
        }
    }
    if (UtilValidate.isNotEmpty(byHourList)) {
        if (!byHourList.contains(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)))) {
            return false;
        }
    }
    if (UtilValidate.isNotEmpty(byDayList)) {
        Iterator<String> iter = byDayList.iterator();
        boolean foundDay = false;
        while (iter.hasNext() && !foundDay) {
            String dayRule = iter.next();
            String dayString = getDailyString(dayRule);
            if (cal.get(Calendar.DAY_OF_WEEK) == getCalendarDay(dayString)) {
                if ((hasNumber(dayRule)) && (getFrequency() == MONTHLY || getFrequency() == YEARLY)) {
                    int modifier = getDailyNumber(dayRule);
                    if (modifier == 0) {
                        foundDay = true;
                    }
                    if (getFrequency() == MONTHLY) {
                        // figure if we are the nth xDAY if this month
                        int currentPos = cal.get(Calendar.WEEK_OF_MONTH);
                        int dayPosCalc = cal.get(Calendar.DAY_OF_MONTH) - ((currentPos - 1) * 7);
                        if (dayPosCalc < 1) {
                            currentPos--;
                        }
                        if (modifier > 0) {
                            if (currentPos == modifier) {
                                foundDay = true;
                            }
                        } else if (modifier < 0) {
                            int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int firstDay = dayPosCalc > 0 ? dayPosCalc : dayPosCalc + 7;
                            int totalDay = ((maxDay - firstDay) / 7) + 1;
                            int thisDiff = (currentPos - totalDay) - 1;
                            if (thisDiff == modifier) {
                                foundDay = true;
                            }
                        }
                    } else if (getFrequency() == YEARLY) {
                        // figure if we are the nth xDAY if this year
                        int currentPos = cal.get(Calendar.WEEK_OF_YEAR);
                        int dayPosCalc = cal.get(Calendar.DAY_OF_YEAR) - ((currentPos - 1) * 7);
                        if (dayPosCalc < 1) {
                            currentPos--;
                        }
                        if (modifier > 0) {
                            if (currentPos == modifier) {
                                foundDay = true;
                            }
                        } else if (modifier < 0) {
                            int maxDay = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
                            int firstDay = dayPosCalc > 0 ? dayPosCalc : dayPosCalc + 7;
                            int totalDay = ((maxDay - firstDay) / 7) + 1;
                            int thisDiff = (currentPos - totalDay) - 1;
                            if (thisDiff == modifier) {
                                foundDay = true;
                            }
                        }
                    }
                } else {
                    // we are a DOW only rule
                    foundDay = true;
                }
            }
        }
        if (!foundDay) {
            return false;
        }
    }
    if (UtilValidate.isNotEmpty(byMonthDayList)) {
        Iterator<String> iter = byMonthDayList.iterator();
        boolean foundDay = false;
        while (iter.hasNext() && !foundDay) {
            int day = 0;
            String dayStr = iter.next();
            try {
                day = Integer.parseInt(dayStr);
            } catch (NumberFormatException nfe) {
                Debug.logError(nfe, "Error parsing day string " + dayStr + ": " + nfe.toString(), module);
            }
            int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            int currentDay = cal.get(Calendar.DAY_OF_MONTH);
            if (day > 0 && day == currentDay) {
                foundDay = true;
            }
            if (day < 0 && day == ((currentDay - maxDay) - 1)) {
                foundDay = true;
            }
        }
        if (!foundDay) {
            return false;
        }
    }
    if (UtilValidate.isNotEmpty(byYearDayList)) {
        Iterator<String> iter = byYearDayList.iterator();
        boolean foundDay = false;
        while (iter.hasNext() && !foundDay) {
            int day = 0;
            String dayStr = iter.next();
            try {
                day = Integer.parseInt(dayStr);
            } catch (NumberFormatException nfe) {
                Debug.logError(nfe, "Error parsing day string " + dayStr + ": " + nfe.toString(), module);
            }
            int maxDay = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
            int currentDay = cal.get(Calendar.DAY_OF_YEAR);
            if (day > 0 && day == currentDay) {
                foundDay = true;
            }
            if (day < 0 && day == ((currentDay - maxDay) - 1)) {
                foundDay = true;
            }
        }
        if (!foundDay) {
            return false;
        }
    }
    if (UtilValidate.isNotEmpty(byWeekNoList)) {
        Iterator<String> iter = byWeekNoList.iterator();
        boolean foundWeek = false;
        while (iter.hasNext() && !foundWeek) {
            int week = 0;
            String weekStr = iter.next();
            try {
                week = Integer.parseInt(weekStr);
            } catch (NumberFormatException nfe) {
                Debug.logError(nfe, "Error parsing week string " + weekStr + ": " + nfe.toString(), module);
            }
            int maxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);
            int currentWeek = cal.get(Calendar.WEEK_OF_YEAR);
            if (week > 0 && week == currentWeek) {
                foundWeek = true;
            }
            if (week < 0 && week == ((currentWeek - maxWeek) - 1)) {
                foundWeek = true;
            }
        }
        if (!foundWeek) {
            return false;
        }
    }
    if (UtilValidate.isNotEmpty(byMonthList)) {
        Iterator<String> iter = byMonthList.iterator();
        boolean foundMonth = false;
        while (iter.hasNext() && !foundMonth) {
            int month = 0;
            String monthStr = iter.next();
            try {
                month = Integer.parseInt(monthStr);
            } catch (NumberFormatException nfe) {
                Debug.logError(nfe, "Error parsing month string " + monthStr + ": " + nfe.toString(), module);
            }
            if (month == cal.get(Calendar.MONTH)) {
                foundMonth = true;
            }
        }
        if (!foundMonth) {
            return false;
        }
    }
    return true;
}
Also used : Calendar(com.ibm.icu.util.Calendar)

Example 67 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class RecurrenceUtil method formatDate.

/**
 * Returns a String from a Date object
 */
public static String formatDate(Date date) {
    String formatString = "";
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    if (cal.isSet(Calendar.MINUTE)) {
        formatString = "yyyyMMdd'T'hhmmss";
    } else {
        formatString = "yyyyMMdd";
    }
    SimpleDateFormat formatter = new SimpleDateFormat(formatString);
    return formatter.format(date);
}
Also used : Calendar(com.ibm.icu.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat)

Example 68 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class ExpressionUiHelper method getLastDayOfWeek.

/**
 * Returns the last day of the week for the specified locale.
 * @param locale
 * @return The last day of the week for the specified locale
 */
public static int getLastDayOfWeek(Locale locale) {
    Calendar tempCal = Calendar.getInstance(locale);
    tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek());
    tempCal.roll(Calendar.DAY_OF_WEEK, -1);
    return tempCal.get(Calendar.DAY_OF_WEEK);
}
Also used : Calendar(com.ibm.icu.util.Calendar)

Example 69 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class TemporalExpression method getRange.

/**
 * Returns a range of dates matching this expression. Returns an
 * empty Set if no dates are found.
 * @param range The range of dates to evaluate
 * @param cal The starting date
 * @return A Set of matching <code>Date</code> objects
 */
public Set<Date> getRange(org.apache.ofbiz.base.util.DateRange range, Calendar cal) {
    Set<Date> set = new TreeSet<>();
    Date last;
    Calendar next = first(cal);
    while (next != null && range.includesDate(next.getTime())) {
        last = next.getTime();
        set.add(last);
        next = next(next);
        if (next != null && last.equals(next.getTime())) {
            break;
        }
    }
    return set;
}
Also used : TreeSet(java.util.TreeSet) Calendar(com.ibm.icu.util.Calendar) Date(java.util.Date)

Example 70 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class ServerHitBin method getEvenStartingTime.

private static long getEvenStartingTime(long binLength) {
    // binLengths should be a divisable evenly into 1 hour
    long curTime = System.currentTimeMillis();
    // find the first previous millis that are even on the hour
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date(curTime));
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    while (cal.getTime().getTime() < (curTime - binLength)) {
        cal.add(Calendar.MILLISECOND, (int) binLength);
    }
    return cal.getTime().getTime();
}
Also used : Calendar(com.ibm.icu.util.Calendar) Date(java.util.Date)

Aggregations

Calendar (com.ibm.icu.util.Calendar)75 Timestamp (java.sql.Timestamp)37 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)24 GenericValue (org.apache.ofbiz.entity.GenericValue)24 Delegator (org.apache.ofbiz.entity.Delegator)17 Date (java.util.Date)14 HashMap (java.util.HashMap)12 Locale (java.util.Locale)12 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)11 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)11 GregorianCalendar (com.ibm.icu.util.GregorianCalendar)10 ArrayList (java.util.ArrayList)8 SimpleDateFormat (java.text.SimpleDateFormat)6 LinkedList (java.util.LinkedList)6 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)6 Map (java.util.Map)5 TimeDuration (org.apache.ofbiz.base.util.TimeDuration)5 BigDecimal (java.math.BigDecimal)4 Time (java.sql.Time)4 UtilDateTime (org.apache.ofbiz.base.util.UtilDateTime)4