You are here

util

Calculating the difference between dates in Java

Calculating the difference in days between two dates in Java sounds like a trivial task, but it turns out to be otherwise. To keep track of time, Java counts the number of milliseconds from the start of January 1, 1970 and this is called the epoch. Many programmers then think that subtracting the millisecond timestamps of two moments and dividing by the number of milliseconds per day is the right way to calculate the difference between two dates. The code below is as simple as it could be, but it is also as wrong. Here is why:

Subscribe to RSS - util