views:

473

answers:

1

Joda-Time library includes different datetime classes

DateTime - Immutable replacement for JDK Calendar
DateMidnight - Immutable class representing a date where the time is forced to midnight
LocalDateTime - Immutable class representing a local date and time (no time zone)

I'm wondering how are you using these classes in your Layered Applications.

I see advantages in having almost all the Interfaces using LocalDateTime (at the Service Layer at least) so that my Application doesn't have to manage Timezones and can safely assume Times always in UTC. My app could then use DateTime to manage Timezones at the very beginning of the Execution's Flow.

I'm also wondering in which scenario can DateMidnight be useful.

+1  A: 

I see advantages in having almost all the Interfaces using LocalDateTime (at the Service Layer at least) so that my Application doesn't have to manage Timezones and can safely assume Times always in UTC.

I'm not sure I understand your line of thinking here. LocalDateTime and DateTime are two different (conceptually) things, and should be used as such. It's not that a LocalDateTime assumes a UTC timezone, it actually doesnt include a timezone (internally it may be represented as a DateTime with UTC timezone, but it's just a implementation issue, it does not matter to the programmer who uses it).

You can see in the API docs that, while a DateTime is a "Instant" (a point in the world time line, a physical concept), a LocalDateTime is NOT: it's a "partial", (a "civil" concept), a different class hierarchy. (It's perhaps unfortunate that the names of the classes makes one think that they are some kind of subclass or specialization)

A LocalDateTime is just a pair {Date (Y/M/D) ; Time (hh:mm:ss.msec)}, a bunch of numbers which corresponds to the "civil" standard representation of time-related data. One can convert from a LocalDateTime to a DateTime, (which would require to specify a timezone), but that conversion takes we to another kind of entity (an analogy: when you convert a String to/from a byte array - specifying a charset encoding).

When to use one or the other in the application... it's sometimes arguable, but frequently is clear enough, once the Jodatime concepts are understood. And IMO is dimly related to "layers", perhaps more to use cases or scenarios.

A non-trivial -borderline- example: You work at Google, programming the Calendar. You must let the user manage (add, see, modify) an event which includes a date-time (lets ignore recurrent events), say "I have an appointement with my doctor on 2012-July-3 at 10:00 am". What is the time-date entity to use in the software layer (for this usecase)? I'd say: a LocalDateTime. Because the user is not really manipulating a physical point in time, but a civil time, the date and time that displays the clock in his wrist or in his home. He does not even think of timezones (lets ignore the special case of a user who is traveling around the world...) Then, in the bussiness and presentation layer, a LocalDateTime seems the right entity.

But you (the lucky Google programmer) must program also a very different scenario: the reminders. When the Google internal scheduler detects that the event stored by the user is N minutes in the future from now, it must send him a reminder. Here, "N minutes from now" is a totally "physical" concept of time, so here the "business layer" would deal with a DateTime. There are several alternatives, for example: the event was stored in the DB as a LocalDateTime (ie. just time and date without timezone - one frequently uses a UTC timestamp to represent that, but this an implementation detail). In this scenario (only in this) we must load it as a DateTime, we convert it using a Timezone, presumably from the user's profile.

leonbloy