Subtract dates in java

0

I would like to be able to subtract two dates that I call from a BBDD but the problem that in some cases does not affect me correctly is the time zone

@Override
public List<Ausentismo> list(Date fechai, Date fechaf, Date fechaMesA, Date fechaMesS) {
    List<Ausentismo> list = null;

    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

    String Fechaf = format.format(fechaf);
    String Fechai = format.format(fechai);
    String FechaMesA = format.format(fechaMesA);
    String FechaMesS = format.format(fechaMesS);

    String sql = "SELECT AU.COD_MOTIVO, AU.CODIGO, AU.FECHA_SALIDA, AU.FECHA_RETORNO, AU.DIAS_CIA \n"
            + "FROM RH_AUSENTISMO AU WHERE FECHA_SALIDA BETWEEN '" + FechaMesA + "' AND '" + Fechaf + "' \n"
            + "AND FECHA_RETORNO BETWEEN '" + Fechai + "' AND '" + FechaMesS + "'";
    try {

        Connection cn = db.getConnection();
        PreparedStatement st = cn.prepareStatement(sql);

        ResultSet rs = st.executeQuery(sql);
        list = new ArrayList<Ausentismo>();
        while (rs.next()) {
            Ausentismo o = new Ausentismo();
            o.setCOD_MOTIVO(rs.getString(1));
            o.setCODIGO(rs.getInt(2));
            o.setFECHA_SALIDA(rs.getDate(3));
            o.setFECHA_RETORNO(rs.getDate(4));
            o.setDIAS_CIA(rs.getInt(5));
            list.add(o);
        }

That's the query and from here the rest

for (int k = 0; k < listA05.size(); k++) {

    if (listp.get(j).getCODIGO() == listA05.get(k).getCODIGO()) {
        e = true;

        if ((listA05.get(k).getFECHA_RETORNO().getTime()) < fechaf.getTime() 
                && listA05.get(k).getFECHA_SALIDA().getTime() > fechai.getTime() )
        {
            rf = (int) ((listA05.get(k).getFECHA_RETORNO().getTime()
                - listA05.get(k).getFECHA_SALIDA().getTime()) / 86400000);

        } else {

            if (listA05.get(k).getFECHA_SALIDA().getTime() < fechai.getTime()) {
                rf = (int) ((listA05.get(k).getFECHA_RETORNO().getTime()
                        - fechai.getTime()) / 86400000);

            } else {
                rf = (int) ((fechaf.getTime()
                        - listA05.get(k).getFECHA_SALIDA().getTime()) / 86400000);
            }

        }
    
asked by Dalstron 18.12.2018 в 05:27
source

0 answers