File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes util.Date to sql.Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "util.Date to sql.Date" Watch "util.Date to sql.Date" New topic
Author

util.Date to sql.Date

Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
I convert java.util.Date to java.sql.Date this way:

java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

And then send sqlDate to prepareStatement.
ps.setDate(1, sqlDate);

I have a problem, a get from a text field string value that i convert to Date with datefromat class.
So, i dont need getTime(), i already have a value in my utilDate, but like that netBeans shows me error.

I need this:

java.sql.Date sqlDate = new java.sql.Date(utilDate);

What is the problem?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Check out what constructors java.sql.Date has. Does any take a java.util.Date or a super class of it?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
Date(long date)
Constructs a Date object using the given milliseconds time value.

But i dont know how to convert my date into long (milisec) so that i can put it in constructor!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Ivan Kesic wrote:I convert java.util.Date to java.sql.Date this way:

java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

And then send sqlDate to prepareStatement.
ps.setDate(1, sqlDate);

I have a problem, a get from a text field string value that i convert to Date with datefromat class.
So, i dont need getTime(), i already have a value in my utilDate,


I think you misunderstand what getTime() does. Your utilDate will have a value in it as soon as it is created (via the date formatter or by the constructor). The getTime() method retrieves that value as a long count of milliseconds from some standard time point. You DO need getTime() because it is the way you move that long value from the utilDate into the sqlDate, regardless of how utilDate was made.

but like that netBeans shows me error.

I need this:

java.sql.Date sqlDate = new java.sql.Date(utilDate);

What is the problem?

The problem is that java.sql.Date doesn't have a constructor which takes a java.util.Date as a parameter.

You should read the API for both the java.util.Date and the java.sql.Date objects. Look how the forum software nicely underlines those names for you? You can click on them and follow the link to the API so you can see what the various constructors are available and what methods do.


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Ivan Kesic wrote:But i dont know how to convert my date into long (milisec) so that i can put it in constructor!

I must miss something, because your opening post suggests you do:
Ivan Kesic wrote:I convert java.util.Date to java.sql.Date this way:

java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());


Also, as a note, to get the current time as a long you can use System.currentTimeMillis(). It's the same as utilDate.getTime() in your example code.
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
I dont need current time. I have a util.Date object with time in format dd/mm/yyyy. I tryed to cast that to sql.Date but it doesnt work.
Then i tryed java.sql.Date sqlDate = new java.sql.Date(utilDate); it still doesnt work... (couse constructor)
Now i have to think of something else...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Have you read your very own post? The part I quoted shows exactly how to do this.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

There seems to be some confusion here.

Ivan, it looks like you are convinced that you should not call getTime() on the java.util.Date object that you have. But that idea is based on a misunderstanding.

You say that you have a java.util.Date format "in format dd/mm/yyyy". That is a mistake. Date objects do not have a format. A Date object is nothing more than a container for a number of milliseconds since 1 January 1970, 00:00:00 UTC. It does not have a format, just like numbers by themselves do not have a format - they are just numbers.

You already know how to make a java.sql.Date from a java.util.Date:

There is no fundamentally different way to do this.

Why exactly do you think you should not call getTime() on the java.util.Date object?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
Doesnt getTime() return current time? Or it gives ms till date that is already stored in date object?
Ill try like that and see what i get in my sql.Date...

edit: Yes it works with getTime() i dont know why i didnt try it in first place,it returns value from date not current date...

Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: util.Date to sql.Date
 
Similar Threads
how to convert java.util.date to java.sql.date
how do i get todays dat without the time
how to parse java.util.Date to java.sql.date ?
Convert java.util.Date to java.sql.Date
java.util.Date to java.sql.Date