3

I am facing an issue in inserting date in YYYY-mm-dd hh:mm:ss format in MySql DB. filed in DB is of type datetime.

I tried it :

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.MINUTE, startMinute);
calendar.set(Calendar.SECOND,01);
Date startTime = calendar.getTime();

String hqlUpdate = "update challenge set start_time = :startTime where challenge_id =  :id";
sessionFactory.getCurrentSession().createSQLQuery(hqlUpdate)
                                 .setDate("startTime", startTime)
                                 .setInteger("id", id).executeUpdate();
4
  • in your entity - challange , have you used @Temporal private Date start_time; what is the error that you get? Commented May 8, 2012 at 6:00
  • try to add a try catch block to capture any exception to find the issue that prevents you from saving the data Commented May 8, 2012 at 6:03
  • yes with start_time i have @Temporal(javax.persistence.TemporalType.TIMESTAMP) and am not getting nay exception , but in DB i can c only date hh:mm:ss are as 00:00:00 always Commented May 8, 2012 at 7:31
  • why you have not accepted answer ? Commented Dec 12, 2017 at 7:18

3 Answers 3

5

Try to use the following.

.setTimestamp("startTime", startTime);
Sign up to request clarification or add additional context in comments.

Comments

0

Here I have specifically tried to format the date object using simpleDateFormat before passing it to the hibernate. check if this works.

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.MINUTE, startMinute);
calendar.set(Calendar.SECOND,01);
Date startTime = dateFormat.parse(dateFormat.format(calendar.getTime();));

String hqlUpdate = "update challenge set start_time = :startTime where challenge_id =  :id";
sessionFactory.getCurrentSession().createSQLQuery(hqlUpdate)
                             .setDate("startTime", startTime)
                             .setInteger("id", id).executeUpdate();

Comments

0

It works for me if use setParameter() instead of setDate() ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.