How to convert java.util.date to java.sql.Timestamp?

I wish to convert the java.util.date to java.sql.Timestamp(set all the hours, minutes, seconds, nanoseconds to 0), how to do this?
LVL 9
william007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
CEHJCommented:
long time = date.getTime();
0
 
CEHJCommented:
Sorry

timestamp = new Timestamp(date.getTime());
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
rama_krishna580Commented:
Hi,

  private final java.sql.Timestamp sqlTimestamp(java.util.Date utilDate)
 {
    return new java.sql.Timestamp(utilDate.getTime());
 }

R.K
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
william007Author Commented:
Thanks:-)
0
 
el_diosCommented:
Hi CEHJ,
I would agree with CEHJ.

You can do it another way if you want to use the constructor you have shown:
Timestamp(int year, int month, int date, int hour, int minute, int second, int nano)
then you would need to do :

Calendar cal = Calendar.getInstance();
cal.setTime(date);
timestamp = new Timestamp(cal.get(Calendar.HOUR), .......,.....);


Cheers!
0
 
CEHJCommented:
:-)
0
 
CEHJCommented:
Zeroing the time is fairly simple:

                  Calendar cal = new GregorianCalendar(2006, 3, 19);
                  java.sql.Timestamp t = new java.sql.Timestamp(cal.getTimeInMillis());
                  System.out.println(t);
0
 
william007Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.