⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 daterangevalidationutils.java

📁 一个很好的开源项目管理系统源代码
💻 JAVA
字号:
package net.java.workeffort.infrastructure.utils;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Collection;import java.util.Date;import java.util.Iterator;import org.apache.commons.beanutils.PropertyUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * Date range validation utils. * @author Antony Joseph */public class DateRangeValidationUtils {    protected static final Log log = LogFactory            .getLog(DateRangeValidationUtils.class);    /**     * In a multi row form, this will validate the date ranges across the     * multiple rows. For example when entering users rates the date ranges     * should not conflict.     * @param rows The filtered rows     * @param fromDt The from date against which the collection of rows will be     *            checked.     * @param thruDt The thru date against which the collection of rows will be     *            checked     * @param fromDtPropertyName The from date property name     * @param thruDtPropertyName The thru date property name     * @return Whether the multi row dates are valid     */    public static boolean validateMultiRowDateRanges(Collection rows,            Date fromDt, Date thruDt, String fromDtPropertyName,            String thruDtPropertyName) {        // Check if the date range does not conflict with the other date ranges.        // Note that the rows list will only have rows which meet a certain        // filter criteria.        if (rows != null && rows.size() > 0) {            Iterator it = rows.iterator();            while (it.hasNext()) {                Object row = it.next();                Date rowFromDt = (Date) getSimpleProperty(row,                        fromDtPropertyName);                if (rowFromDt == null)                    rowFromDt = getDefaultFromDate();                Date rowThruDt = (Date) getSimpleProperty(row,                        thruDtPropertyName);                if (rowThruDt == null)                    rowThruDt = getDefaultThruDate();                if (log.isDebugEnabled())                    log.debug("Checking date ranges: fromDt=" + fromDt                            + " thruDt=" + thruDt + " rowFromDt=" + rowFromDt                            + " rowThruDt=" + rowThruDt);                if (((fromDt.getTime() >= rowFromDt.getTime()) && (fromDt                        .getTime() <= rowThruDt.getTime()))                        || ((thruDt.getTime() >= rowFromDt.getTime()) && (thruDt                                .getTime() <= rowThruDt.getTime()))) {                    return false;                }            }        }        return true;    }    public static Date getDefaultFromDate() {        Calendar calendar = Calendar.getInstance();        calendar.set(1800, 1, 1);        return calendar.getTime();    }    public static Date getDefaultThruDate() {        Calendar calendar = Calendar.getInstance();        calendar.set(3999, 12, 31);        return calendar.getTime();    }    public static Date getDate(SimpleDateFormat parser, String dateStr) {        try {            return parser.parse(dateStr);        }        catch (ParseException pe) {            throw new RuntimeException("Date parsing error: value=" + dateStr,                    pe);        }    }    private static Object getSimpleProperty(Object obj, String propertyName) {        try {            return PropertyUtils.getSimpleProperty(obj, propertyName);        }        catch (Exception e) {            throw new RuntimeException("Error while getting property:"                    + propertyName + " from object:" + obj, e);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -