views:

81

answers:

6

And wait, don't rush to answer "java.util.Date", consider the following scenario.

Person object having 2 fields: "birthday" and "nextMeeting" both java.util.Date. Now birthday stored in database as date type column (no time) for eg. 01-10-1979, and nextMeeting as datetime type for ex. 01-10-2010 20:00:00.

You pull it from db, "birthday" will be auto set to midnight by JDBC. Now you need to send this object to other JVM using lets say RMI or whatever technology.

On the other end JVM has timezone -1h from originating JVM. This is where problem starts. nextMeeting become 01-10-2010 19:00:00 which is absolutely FINE and CORRECT from user perspective etc...

BUT birthday become 31-09-1979 23:00:00 which will be represented to user as 31st of September, which is really not what we want, cause obviously birthday is something static and NOT dependent on timezones.

So column type in db chosen correctly (date). This type of column usually represented as java.util.Date. But in our case it is wrong java type to use.

So how would you represent a birthday? Consider that you need to manipulate this object on a UI like in a datepicker component etc...

A: 

Somehow, the two java systems will have to agree on Calendar/TimeZone information, or the Date object will need to be converted to a timestamp when being passed to the remote system.

The simplest method might be to simply require all clients to treat the birthday as a GMT time --- when they display/compare/whatever the birthdays, have them create a Calendar with the "GMT" TimeZone, and then setTime() on it with the supplied Date.

If you're working with the model locally at all, you should really have a Date object, not just a timestamp.

gnud
@Gnud: AFAIU, functionality is working like charm, its the birthday which is special, hence should be immutable. Means no change whatsoever.
Adeel Ansari
Yes....? I was talking about the birthday, and making both systems agree on an interpretation.
gnud
A: 

Introduce a String date and keep both in sync, however give precedence to date of String type whenever there is a difference.

Adeel Ansari
+2  A: 

Use LocalDate from JodaTime and only store the date for the birthday, not the time.

Jeroen Rosenberg
Yeah, good to go. The only concern can be having JodaTime in both JVMs. But again that might not that big of an issue.
Adeel Ansari
Looks like the way to go. I found it is possible to make hibernate understand this type, but I did not find so far is that possible to convert LocalDate back to date to use inside DatePicker components.
Ilja S.
So will consider this as an answer, how ever this not helping me in my particular case. To simplify question I did not mention that "other end" is actually GWT client side, hence is JS, therefore not having Joda :(
Ilja S.
If the other end is GWT - consider using plain text or goda time (http://code.google.com/p/goda-time/)
krtek
A: 

For manipulation i will advise java.util.Calendar

For representation

Birthday as java.util.Date
NextMeetin as java.sql.Timestamp

Vash
A: 

if you are dealing with dates, you are better of using joda time. You can build a DateTime object with date/time and timezone info so you have all info needed to deal with different timezones.

raticulin
DateTime object contains time -> hence it will not work. @Jeroens answer is right.
krtek
A: 

That's a very good question...

Android for example stores birthday as String in 'yyyy-MM-dd' format. I was wondering why they don't use java.util.Date and I guess that the reason would be the same problem which you brought here.

So I would recomment either String or some "Timezone-independent-date". But after few minutes searching in Java and joda-time documentation I have no idea how to do it.

EDIT: Seems @Jeroen is right - use LocalDate.

krtek