Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 74261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:19:50+00:00 2026-05-10T20:19:50+00:00

How can i map a date from a java object to a database with

  • 0

How can i map a date from a java object to a database with Hibernate? I try different approaches, but i am not happy with them. Why? Let me explain my issue. I have the following class [1] including the main method i invoke and with the following mapping [2]. The issue about this approach you can see, when you look at the console output.

false

false

1

-1

1224754335648

1224754335000

Thu Oct 23 11:32:15 CEST 2008

Clock@67064

As you can see the the to dates are not exactly equal, although they should, so it is hard to compare them without goofing around with return value of getTime. I also tried java.sql.Date, Timestamp and date instead of timestamp in the mapping, but without success.

I wonder why the last three digits are zero and if this is a hibernate or a java issue or my own stupidity.

Thank you for reading.

[1]

public class Clock {      int id;     java.util.Date date;      public static void main(String[] args) {         HibernateUtil.init();         HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();          Clock clock = new Clock();         clock.date = new java.util.Date();          HibernateUtil.getSessionFactory().getCurrentSession().saveOrUpdate(clock);         HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();          HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();          Clock fromDBClock = (Clock)HibernateUtil.getSessionFactory()                         .getCurrentSession().get(Clock.class, 1);          System.out.println(clock.date.equals(fromDBClock.date));         System.out.println(fromDBClock.date.equals(clock.date));          System.out.println(clock.date.compareTo(fromDBClock.date));         System.out.println(fromDBClock.date.compareTo(clock.date));          System.out.println(clock.date.getTime());         System.out.println(fromDBClock.date.getTime());          System.out.println(clock.date.toString());         System.out.println(fromDBClock.toString());          HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();          HibernateUtil.end();     }      public int getId() {         return id;     }      public void setId(int id) {         this.id = id;     }      public java.util.Date getDate() {         return date;     }      public void setDate(java.util.Date date) {         this.date = date;     }   } 

[2]

<?xml version='1.0'?> <!DOCTYPE hibernate-mapping PUBLIC         '-//Hibernate/Hibernate Mapping DTD 3.0//EN'         'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>  <hibernate-mapping>      <class name='Clock' table='CLOCK'>         <id name='id' column='CLOCK_ID'>             <generator class='native'/>         </id>         <property name='date' type='timestamp'/>       </class>  </hibernate-mapping> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:19:50+00:00Added an answer on May 10, 2026 at 8:19 pm

    MySql DateTime precision is only to the second. Java Date precision is to the millisecond. That is why the last three digits are zeros after it has been put in the database.

    Do this to your original Date:

    date = date.setTime((date.getTime() / 1000) * 1000);

    This will set it to the last exact second, and all your comparisons will then match.

    (BTW, System.out.println(fromDBClock.toString()); should be System.out.println(fromDBClock.date.toString());

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could use a mod_rewrite command in your .htaccess to… May 11, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer Put the javascript right after the element, that will minimise… May 11, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer Okay, let's see. This is where the developer tools come… May 11, 2026 at 10:09 pm

Related Questions

How do I segment a 2D image into blobs of similar values efficiently? The
I have a huge mbox file, with maybe 500 emails in it. It looks
I have a Oracle database and one of the fields is a date range
Here is the domain that I wish to have: public class Person { public

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.