• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

util.Date to sql.Date

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read your very own post? The part I quoted shows exactly how to do this.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic