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;
}
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);
}
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);
}
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;
}
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();
}
Aggregations