File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Why java.sql.Date? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Why java.sql.Date?" Watch "Why java.sql.Date?" New topic

Why java.sql.Date?

Sainudheen Mydeen
Ranch Hand

Joined: Aug 18, 2003
Posts: 218
There are two Date classes. java.util.Date and java.sql.Date. All I could see additional in sql.Date is the method valueOf(String s) which Converts a string in JDBC date escape format to a Date value. Rest all available in util.Date. Can some body give me an idea why sun is keeping sql.Date? (Just for one function?). Or I misunderstood? Also when to select which Date class?
[ October 08, 2003: Message edited by: Sainudheen Mydeen ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

java.sql.Date is newer than java.util.Date. The one sql-related function would be inappropriate to add to java.util.Date, and using a class from java.sql would be out of place in non-JDBC code, so having both classes is worthwhile. The java.sql version also restricts the range of available constructors.

[Jess in Action][AskingGoodQuestions]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
From the above link:

Like java.util.Date, all three classes in the SQL package are thin wrappers around a numeric value representing a point in time. The Date and Time classes ignore the information regarding the time of day or the calendar date, respectively.
The Timestamp class, however, not only includes the usual time and date information up to millisecond precision, but also allows storing additional data to accurately represent a point in time with nanosecond precision. (A nanosecond is a billionth of a second.)

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
what i understand is that, java.sql.Date can used to compare the date value that return from query statement whereas java.util.Date cannot function this.
Jason Menard

Joined: Nov 09, 2000
Posts: 6450
The thing to keep in mind with a java.sql.Date is that the hours, minutes, seconds, and milliseconds are set to 0. I believe this was't the case prior to JSDK 1.4, but it was fixed in order to comply with the SQL standard, where SQL Date values don't have time components. If you need finer granularity than that provided by java.sql.Date, use java.sql.TimeStamp.
[ October 09, 2003: Message edited by: Jason Menard ]
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33132

I believe this was't the case prior to JSDK 1.4, but it was fixed in order to comply with the SQL standard, where SQL Date values don't have time components.

It is fixed in 1.3 as well.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Why java.sql.Date?
It's not a secret anymore!