?
Solved

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

Posted on 2006-04-19
8
Medium Priority
?
1,942 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:william007
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16488283
long time = date.getTime();
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16488295
Sorry

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

Expert Comment

by:rama_krishna580
ID: 16488439
Hi,

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

R.K
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:william007
ID: 16488496
Thanks:-)
0
 
LVL 3

Expert Comment

by:el_dios
ID: 16488510
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16488512
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16488527
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
 
LVL 9

Author Comment

by:william007
ID: 16488536
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question