Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

util.Date to sql.Date

 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out what constructors java.sql.Date has. Does any take a java.util.Date or a super class of it?
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read your very own post? The part I quoted shows exactly how to do this.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic