Showing posts with label Joda Time. Show all posts
Showing posts with label Joda Time. Show all posts

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;

Monday, April 21, 2008

Joda Time

First things first, nothing to do with Yoda (even if at first glance somebody might be misleaded). Joda Time is a library which should replace the Java date and time classes (who's never clashed with them?). At the moment I'm just starting to play with it, tired of... the dark side. The library also offers the Interval class, that represents a time period between two timepoints. What I still have to find out is how to manage open intervals.