Saturday, April 17, 2010

Joda Time vs java.sql.Date

As Naked Objects does not (yet) support the Joda Time library, I'm using plain old java.sql.Date to model dates in a prototype I'm working on. I'm not used to it anymore, and that's awful. As an example, let's suppose we have to create an entity with two Date properties, say creationDate and dueDate: the former is asked to the Clock class from the app library, and the latter is derived adding two working days to the former one. For the sake of simplicity, let's only consider adding two days, regardless of the fact that they are working days or not (you always can take it as an exercise).


Calendar cal = Clock.getTimeAsCalendar();
Date creationDate = new Date(cal.getTimeInMillis());
cal.add(Calendar.DAY_OF_MONTH, 1);
Date dueDate = new Date(cal.getTimeInMillis());

Now, with Joda Time I would just write


LocalDate creationDate = new LocalDate(Clock.getTime());
LocalDate dueDate = creationDate.plusDays(2);

Does it compare to the traditional Date and Calendar approach? I think it doesn't...Moreover, should Joda Time be supported, we could also ask for a factory method:

LocalDate creationDate = Clock.getLocalDate;

No comments: