dagblog.
← Archive

Programming puzzle: dates


Does your API have date utility methods? If so, the answer to the following quiz may surprise you a little. Suppose we have a method that subtracts two dates and returns the result in milliseconds.

import java.util.Date;
public long subtract(Date d1, Date d2) {
if(d1 == null || d2 == null) {
throw new IllegalArgumentException();
}
return d1.getTime() - d2.getTime();
}
The question is where the problem lies and how to fix it.

Medium version

Related articles

Related CZ Podcast episodes