Friday, January 25, 2008

EJB3 - Temporal Types

With EJB3 (and jpa, etc) dates can be a little confusing. If you need just the date (no time), use @Temporal(TemporalType.DATE) and you will only get thet date. The time will be zeroed out.

If you want the date with the time, use @TemporalType(TemporalType.TIMESTAMP) and you will get both the date and time.

In both cases, still use java.util.Date or java.util.Calendar (I use java.util.Date). With the eclipse ide, when reverse engineering tables to entities, by default it will plug in java.util.Date with the TemporalType.DATE and you lose the time.

Anyway, hope this helps!

4 comments:

Mike said...

This was very helpful for me. Thanks for posting.

Unknown said...

great post :) you saved my time thanks a lot !

Mario Batres said...

Thanks man, you helped me so much

Avishek Ganguly said...

Suppose my client is in India and the ejb is in Germany. Client is passing only the date. The server has to store it in DB. Will temporal solve the time difference problem between India and Germany?