Add or subtract the time difference between two dates in java

0

I need to add or subtract the time difference between dates, If the ESTADOLINEA is 1 add, and if the ESTADOLINEA is 2, subtract the time.

This is the SQL table:

My idea is to loop through the records

This is my code:

private void fisico(ResultSet rs) {
    try {
        while (rs.next()) {
            if (rs.getRow() != 0) {
                //si es 2 la maquina esta parada, si NO la maquina esta MARCHA 
                if (rs.getInt(2) == 2) {
                    System.out.println(rs.getInt(1) + " Maquina parada " + rs.getInt(2));
                    fechaInicio = String.valueOf(rs.getTimestamp(4));
                } else {
                    System.out.println(rs.getInt(1) + " Maquina en marcha " + rs.getInt(2));
                }
            } else {
            }
        }
    } catch (SQLException ex) {
        System.err.println("" + ex);
    }
}
    
asked by R.Priego 03.05.2017 в 11:54
source

2 answers

1

You can see all the available methods for the Class Timestamp link

To make operations between dates as easy as possible, whatever the language is, obtain the representation in UNIX (milliseconds) and then carry out the operations.

If you want to get the current (system) date in milliseconds you will have to do this:

(int) (System.currentTimeMillis() / 1000L);

To get the milliseconds from a timestamp you have to use the following method:

link

    
answered by 03.05.2017 / 12:39
source
0

You can request the data already processed from the bd

  SELECT if ( estado = 1, (unix_timestamp(fecha1)-unix_timestamp( sysdate() ), unix_timestamp(fecha1)+unix_timestamp( sysdate() ) ) as diferencia FROM tabla

and you can filter (if you want) right there

WHERE diferencia > 3600

for example

    
answered by 03.05.2017 в 16:44