Answer
java.sql.Date descends from java.util.Date, but uses only the year, month and day values. There are two methods to create a Date object. The first uses a Calendar object, setting the year, month and day portions to the desired values. The hour, minute, second and millisecond values must be set to zero. At that point, Calendar.getTime().getTime() is invoked to get the java.util.Date milliseconds. That value is then passed to a java.sql.Date constructor:
Calendar cal = Calendar.getInstance();
// set Date portion to January 1, 1970
cal.set( cal.YEAR, 1970 );
cal.set( cal.MONTH, cal.JANUARY );
cal.set( cal.DATE, 1 );
cal.set( cal.HOUR_OF_DAY, 0 );
cal.set( cal.MINUTE, 0 );
cal.set( cal.SECOND, 0 );
cal.set( cal.MILLISECOND, 0 );
java.sql.Date jsqlD =
new java.sql.Date( cal.getTime().getTime() );
The second method is java.sql.Date's valueOf method. valueOf() accepts a String, which must be the date in JDBC time escape format - "yyyy-mm-dd". For example,
java.sql.Date jsqlD = java.sql.Date.valueOf( "2010-01-31" );
creates a Date object representing January 31, 2010. To use this method with a Calendar object, use:
java.sql.Date jsqlD = java.sql.Date.valueOf(
cal.get(cal.YEAR) + ":" +
cal.get(cal.MONTH) + ":" +
cal.get(cal.DATE) );
which produces a Date object with the same value as the first example.
Is this item
helpful? yes no
Previous votes Yes: 8 No: 0
|
|
Comments and alternative answers
 |
Wow, so complicated... !!!
Alessandro A. Garbagnati PREMIUM, May 15, 2001 [replies:2]
Joe,
Your solution it's way complex and, performance wise, not better than this:
Calendar calendar = new GregorianCalendar(2001, 0, 1);
java.sql.Date date = new java.sql.Date(calendar.getTimeInMillis());
or, if you like it on one line:
java.sql.Date date = new java.sql.Date((new GregorianCalendar(2001, 0, 1)).getTimeInMillis());
Is this item
helpful? yes no
Previous votes Yes: 0 No: 1
|
|

|
 |
 |
Re: Wow, so complicated... !!!
Mekaiel Hussain, Jun 5, 2001 [replies:1]
java.sql.Date date = new java.sql.Date(calendar.getTimeInMillis());
This will not work in Java as you get this at compile time
"XmlSrmCreator.java": Error #: 306 : method getTimeInMillis() has protected access in class java.util.Calendar at line 180, column 57
Is this item
helpful? yes no
Previous votes Yes: 1 No: 0
|
|

|
 |
 |
 |
Re: Re: Wow, so complicated... !!!
Alessandro A. Garbagnati PREMIUM, Jun 5, 2001
Hi,
Yes, I forgot the fact that it's protected...
Well, no problem, just use getTime()...
Calendar calendar = new GregorianCalendar(2001, 0, 1);
java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
or, in one line:
java.sql.Date date = new java.sql.Date((new GregorianCalendar(2001, 0, 1)).getTime().getTime());
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
 |
Convert current System date into a Date object
Mekaiel Hussain, Jun 5, 2001 [replies:3]
Calendar cal = Calendar.getInstance();
cal.set( cal.YEAR, cal.get
(Calendar.YEAR) );
cal.set( cal.MONTH, cal.get(Calendar.MONTH));
cal.set( cal.DATE, cal.get(Calendar.DATE));
Date currentDate = new Date(cal.getTime().getTime());
System.out.println(currentDate.toString());
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
 |
Normalization
Joe Sam Shirah PREMIUM, Mar 23, 2002 [replies:3]
The point of the "so complicated" code is to properly create a normalized Date that works correctly in all circumstances. java.util.Date, which underlies java.sql.Date, has some major flaws. If one creates a java.sql.Date by just grabbing milliseconds, as most of the suggestions do, one will have serious problems with date comparisons and arithmetic.
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
 |
 |
 |
Re[2]: Normalization
Yishai Hornbacher, Oct 7, 2002 [replies:1]
Actually, the above does not account for time zones. That is just a warning, but having the Calendar take care of it for you can be useful.
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
 |
 |
 |
 |
Re[3]: Normalization
David Thirakul, Oct 18, 2004
Indeed, it doesn't consider time zones or locale values.
Use Calendar especially if you need to manipulate your date.
Is this item
helpful? yes no
Previous votes Yes: 1 No: 0
|
|

|
 |
Correction to second solution of the original message
Bob Goldberg, Apr 9, 2007 [replies:1]
In the second method presented in the original message, a String is constructed in the java.sql.date format. However, the code shown incorrectly places colons (":") between the fields; it should use dashes ("-") between the fields.
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
 |
 |
Re: Correction to second solution of the original message
Mushtaq Mathur, Dec 30, 2009
java.sql.Date dt = new java.sql.Date(System.currentTimeMillis());
java.sql.Date dt1 = java.sql.Date.valueOf( "2009-12-12" );
//vo1.setDobDate("12-12-2009");
//vo1.setEffDate("12-12-2009");
//vo1.setExpDate("12-12-2009");
vo1.setDobDate(dt);
vo1.setEffDate(dt);
vo1.setExpDate(dt);
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

|
|
|
 |
|