As I see you want to validate a range of dates, I'll give you an example to validate the initial date and a final date where they do not exceed 30 days and are not longer than today's date.
First of all in the Managed Bean define the properties to use:
private boolean fechaInicialFinalMayoraHoy;
private boolean fechaInicialyFinMayor30Dias;
public static final int NEGATIVO_TREINTA = -30;
private Date fechaInicio;
private Date fechaFinal;
Add their respective accessor methods. If you look, we have two properties of type Date , in these will be stored the start and end date that we want to compare.
Now we add the method that will be responsible for validating the initial date and the end date:
public void validarFechas() throws Exception {
fechaInicialFinalMayoraHoy = false;
fechaInicialyFinMayor30Dias = false;
if (this.fechaInicio != null && this.fechaFinal != null) {
if (isMayorFechaHoy(this.fechaInicio)
|| isMayorFechaHoy(this.fechaFinal)) {
fechaInicialFinalMayoraHoy = true;
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Fecha Inicio o Fecha Fin es mayor a la fecha de hoy"));
}
if (isMayor30Dias(this.fechaInicio, this.fechaFinal)) {
fechaInicialyFinMayor30Dias = true;
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Fecha es mayor a 30 dias."));
}
}
}
First we validate that the fields of type date are! = of null, later it is validated that the date is not greater than today's date (sysdate), the next validation will be that the date ranges are not greater than 30 days .
We add the code of the Boolean method that is responsible for verifying that the date entered is not greater than today's date.
/**
* Descripción de la regla: Si la fecha inicial o la fecha final es
* mayor a la fecha acutal. la validació termina como incorrecta
*
* @param fecha La fecha inicial de consulta o la fecha final de consulta .
* @throws Exception Si las fechas violan la regla de negocio.
*/
public Boolean isMayorFechaHoy(Date fecha) throws Exception {
Date hoy = new Date();
Boolean resultado = false;
if (fecha != null && fecha.after(hoy)) {
resultado = true;
}
return resultado;
}
And finally, a method that validates the range of the two dates entered:
/**
* Descripción de la regla: Si la fecha final es mayor a 30
* días de la fecha inicial la validació termina como
* incorrecta
*
* @param fechaInicial La fecha inicial de consulta.
* @param fechaFinal final de consulta.
* @throws Exception Si las fechas violan la regla de negocio.
*/
public Boolean isMayor30Dias(Date fechaInicial, Date fechaFinal) throws Exception {
Date fhFinal = fechaFinal;
Boolean resultado = false;
if (fechaInicial != null && fhFinal != null) {
Calendar c = Calendar.getInstance();
c.setTime(fhFinal);
c.add(Calendar.DATE, NEGATIVO_TREINTA);
fhFinal = c.getTime();
if (fechaInicial.getTime() < fhFinal.getTime()) {
resultado = true;
}
}
return resultado;
}
With this we end up on the back end, now let's see the view, we will have our XHTML file very simple and it looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:head>
<f:facet name="first">
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
<title>Validar rango de fechas</title>
</f:facet>
</h:head>
<h:body>
<h:form>
<h:panelGrid id="grid" columns="1" cellpadding="1">
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
<p:outputLabel for="fechaInicio" value="Fecha Inicio" />
<p:calendar id="fechaInicio" value="#{validarFechas.fechaInicio}" showOn="button" />
<p:outputLabel for="fechaFin" value="Fecha Inicio" />
<p:calendar id="fechaFin" value="#{validarFechas.fechaFinal}" showOn="button" />
<p:commandButton value="Validar" actionListener="#{validarFechas.validarFechas}" />
</h:panelGrid>
</h:form>
</h:body>
</f:view>
</html>
Now to do deploy. Once there is deploy OK, we will try entering a range of dates that is greater than 30 days:
By pressing the validate button, we will get the error:
Now let's enter a date greater than today:
When validating we see the error:
Versión Prime faces usada:
Prime Faces 5.0
Glass Fish server 4.1.1
JSF:2.2