Validate that a range of dates is not already in a table of date ranges (from-to)

1

What I need is to verify that a new date with a new rank is not between dates that were previously entered in an example database.

fecha_inicio    fecha_fin
10-10-2010      15-10-2010
16-10-2010      19-10-2010

If I wanted to enter a date like

17-10-2010     20-10-2010

to send me a message saying that the fecha_inicio is already in a range.

I have this part of the code.

@SuppressWarnings("null")
public String validarFechasEncuestas( String fecha_pub1, String fecha_pub_fin1, boolean activo)
{
    activo =false;
    String validarEncuesta = "";
    String queryvalidarEncuesta = "";
    queryvalidarEncuesta = "select * from encuestas where activo = "
                + activo + "true order by fecha_creacion" ;


    Calendar fechaInicio = Calendar.getInstance();
    fechaInicio.set(2017, 03, 05);

    Calendar fechaFinal = Calendar.getInstance();
    fechaFinal.set(2017, 03, 10);
    fechaFinal.add(Calendar.DATE, 1);

    Date fecha_pub = null;
    Date fecha_pub_fin = null;

    try
    {
        ResultSet rs = con.ejecutaQuery(queryvalidarEncuesta);
        while(rs.next())
        {
             while (fechaInicio.getTime().before(fechaFinal.getTime())) {
                 System.out.println(fechaInicio.getTime());
                 fechaInicio.add(Calendar.DATE, 1);

                     if(fecha_pub.after(fecha_pub_fin) && fecha_pub_fin.before(fecha_pub) ){
                         activo = false;
                     }
                }

        }
        validarEncuesta = rs.getString("count");
    }
    catch (Exception e)
    {
        System.out.println("Hola tuviste un error");
    }
    return validarEncuesta;
}
    
asked by Francisco Barrientos 07.03.2017 в 17:30
source

2 answers

-1

You do not specify the database you use, but if you use some sql, you can make a query like

SELECT COUNT(*) FROM tu_tabla 
WHERE rango_inicio_que_deseas BETWEEN fecha_inicio AND fecha_fin
OR rengo_fin_que_deseas BETWEEN fecha_inicio AND fecha_fin

If a column returns something greater than 0, it already exists in some range and you can say that the insert is not made

    
answered by 07.03.2017 в 17:39
-1

You need to make a first query to tell you if that record exists:

SELECT COUNT(ID) /* Cualquier campo de tu tabla */
  FROM Tu_Tabla
 WHERE 'Fecha_proporcionada_inicio' BETWEEN fecha_inicio AND fecha_fin
       OR 'Fecha_proporcionada_fin' BETWEEN fecha_inicio AND fecha_fin

This query will return a number that you will have to evaluate; if it is greater than 0, then that range already exists.

    
answered by 07.03.2017 в 17:45