Home arrow static arrow Java Programming [Archive] - java.sql.Date constructor
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - java.sql.Date constructor
This topic has 8 replies on 1 page.

Posts:266
Registered: 2/27/04
java.sql.Date constructor  
Jul 13, 2004 6:42 AM



 
I am experimenting with java.sql.Date.

I tried to construct an instance of Date using the following code.

Date myDate = new Date();

I get the following compiler error.

mainTest.java [29:1] cannot resolve symbol
symbol : constructor Date ()
location: class java.sql.Date
Date myDate = new Date();

I also tried java.sql.Date myDate = new java.sql.Date() and got the same results.

This same code works fine with java.util.Date.
Anyone know how to get this to construct with java.sql.Date?

Thanks.

^
 

Posts:21,719
Registered: 98-02-20
Re: java.sql.Date constructor  
Jul 13, 2004 6:50 AM (reply 1 of 8)



 
I don't believe java.sql.Date has a default ctor. You have to give it a long value for the current time:

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


Read the javadocs.

%
 

Posts:266
Registered: 2/27/04
Re: java.sql.Date constructor  
Jul 13, 2004 6:55 AM (reply 2 of 8)



 
I tried the following but when I printed it out using system.out.println I get a date with no time.

Here is the code:
Date myDate = new java.sql.Date(new java.util.Date().getTime());
Date myDate2 = new java.sql.Date(System.currentTimeMillis());

I tried both of these and got the same results, date with no time.
 

Posts:580
Registered: 8/21/02
Re: java.sql.Date constructor  
Jul 13, 2004 7:01 AM (reply 3 of 8)



 
java.sql.Date myDate = new java.sql.Date(System.currentTimeMillis());
 

Posts:580
Registered: 8/21/02
Re: java.sql.Date constructor  
Jul 13, 2004 7:02 AM (reply 4 of 8)



 
****, how did I delay that much? Anywhays, ignore me.
 

Posts:470
Registered: 6/11/02
Re: java.sql.Date constructor  
Jul 13, 2004 7:02 AM (reply 5 of 8)



 
read tha javadocs:

java.sql
Class Date
java.lang.Object
java.util.Date
java.sql.Date
All Implemented Interfaces:
Cloneable, Comparable, Serializable

public class Date
extends Date

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

 

Posts:266
Registered: 2/27/04
Re: java.sql.Date constructor  
Jul 13, 2004 7:05 AM (reply 6 of 8)



 
java.sql.Date myDate4 = new java.sql.Date(System.currentTimeMillis());

returns a date with no time.

The following code returns a time.

Timestamp myDate3 = new java.sql.Timestamp(new java.util.Date().getTime());

How can I format my time to midnight using java.sql.Timestamp or java.sql.Date ?
 

Posts:21,719
Registered: 98-02-20
Re: java.sql.Date constructor  
Jul 13, 2004 7:30 AM (reply 7 of 8)



 
Right, because java.sql.Date is always normalized to midnight. There IS no time. (Read the javadocs.)

You need java.sql.Timestamp for that.

If you want midnight on a given Date, java.sql.Date will already give you that.

%
 

Posts:266
Registered: 2/27/04
Re: java.sql.Date constructor  
Jul 13, 2004 7:44 AM (reply 8 of 8)



 
I don't understand how I am getting that already with java.sql.Date.
 
This topic has 8 replies on 1 page.