Tobias Hübner
Tobias Hübner

Reputation: 33

Weird behaviour Datediff java Datediff sql

when I try to calculate the difference between two dates in seconds in java I get a different output than in sql.

SQL:

select DATEDIFF(second, 
                 convert(datetime, '2014-01-07 11:00:27', 120), 
                 convert(datetime, '2018-07-09 00:00:00', 120))

=> 142001973

    SimpleDateFormat NORM_DATE_TO_STRING = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Date reference = NORM_DATE_TO_STRING.parse("2014-01-07 11:00:27");

    Date we =  NORM_DATE_TO_STRING.parse("2018-07-09 00:00:00");

    long diff = (we.getTime()-reference.getTime())/1000;

=> 141998373

Its off by one hour and I dont know why.

Thanks in advance

Upvotes: 2

Views: 59

Answers (1)

Zag
Zag

Reputation: 638

The Java version takes daylight savings time into account, and the SQL one doesn't.

Upvotes: 2

Related Questions