ArticleS. UncleBob.
JavaDates [add child]

Java Dates

This is just a rant about the horrible state of Dates in the Java Standard Library. Before you react, be assured we know about Joda. Our problem is that we are working for a client that (for one reason or another) cannot tolerate the use of open-source in their projects.

I am working with my compatriots on a simple Java application for managing a Library. Yesterday I wrote the module that calculated fines for overdue books. To do this properly I needed to know the number of days between the current time and the due-date. This is a matter of simply subtracting the two dates. However, the Java Standard Library makes this grossly difficult.

The two dates are kept as Date objects. In order to subtract them I was forced to:
  1. Convert them to Calendar objects
  2. Clear the times of both to midnight
  3. Convert them to milliseconds since the epoch
  4. subtract the milliseconds
  5. divide by 86,400,000 (The number of milliseconds in a day)

Here's the code I used.

private int daysBetween(Date now, Date returnDate) {
Calendar cNow = Calendar.getInstance();
Calendar cReturnDate = Calendar.getInstance();
long todayMs = cNow.getTimeInMillis();
long returnMs = cReturnDate.getTimeInMillis();
long intervalMs = todayMs - returnMs;
return millisecondsToDays(intervalMs);

private int millisecondsToDays(long intervalMs) {
return (int) (intervalMs / (1000 * 86400));

private void setTimeToMidnight(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

There are probably better ways; but this was all I could think of yesterday afternoon at the end of the iteration. Still, one would think that with all the tremendous thought and and effort that was put into the Java Standard Library, someting as basic to software as subtracting two dates would have been considered.

This is not al new problem. I've faced it before. And I've used other libraries to solve it before. I just happen to be in a unique position at the moment.

When I look at the Date/Calendar classes in the Java Standard Libarary I am reminded of the lesson that I learned 10 years ago while trying to build a reusable library in C++; and of Stroustrup's words of wisdom at the time. The only reusable libraries are libraries that are reused. In other words, if you set out to build a reusable library by thinking real hard you are likely to fail. On the other hand, you can build a successful reusable library by participating in several active projects and negotiating real useful elements for them.

I don't know how the Date/Calendar classes were built; but they show the hallmarks of thinking real hard. And I, at least, find them somewhat less then useful.

I've heard it said that the difference between useful software and worthless crap is that people build useful software for themselves, and build worthless crap for other people to use. It still looks like a sound theory. When I look at the Java date libraries or MFC or any of a dozen other such efforts, and then turn around and look at perl, python, ruby, even C++ I see proof of the concept. Even useful things can grow out of bounds (C++) but at least they're useful.