• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: MySql DATETIME to java.util.Date

 
Rick Beaver
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Possibly a quick question.

I am mapping a MySql datetime field to a java.util.Date field but only the date part is being set, I lose all time information from the DB.

My field is:

Name: due
Datatype: DATETIME

My mapping line is:

<property name="dateDue" type="date" column="due"/>


I have two rows. The due field is set to:

2005-08-02 10:22:00
2005-08-02 10:23:00

So I end up with java.util.Date objects, both with the date as 2005-08-02 but the time part is blank. If I print dueDate.getTime() I end up with:

1122850800000
1122850800000

So the milliseconds is the same for both objects.

Can anyone help me please?

MySql: 4.1.9
Hibernate: 3.0.5
MySql Driver: MySql connector 3.0.14

Many thanks in advance.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reached from search engine here...

My date on jsp is ----- 02-01-2008

I am retriving it on server side and setting it in my object...


After hibernation the contents in myql table are. 2007-12-29

Also at times when retrieving back the data from date column in mysql and formatting the date object using same formatter mm-dd-yyy and printing the string doesn't work properly. Month is retrieved as '00' and year being reduced by 1.

Any suggestions...
[ June 18, 2008: Message edited by: Akhilesh Trivedi ]
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in the annotations world, we have the TemporalType annotation. This can be set to one of three values: TIME, DATE or TIMESTAMP. You probably need to use the equivalent in your mapping file.

Check out the Hibernate JavaDoc for a number of classes that can be used to help you out when working with dates:

Hibernate JavaDoc for TimeType

You might find the TimeType, TimeZoneType and TimestampType helpful when working with java.util and java.sql Date objects.

-Cameron McKenzie
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output of above one is

2008-06-25 00:00:00.0 <---- this is perfectly retrieved from MySQL DB
00-25-2008 <---- has trouble with formatting (month always formats to '00')



Should hibernation be cause for this?
[ June 19, 2008: Message edited by: Akhilesh Trivedi ]
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never thought it could be that silly... 'm' in the format would give me minutes and 'M' (capital) would give me month.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic