Newbie
I am trying to retrieve the system date in the date format dd/mm/yyyy
to use in an sql statement.
The function i am using is returning a date format of: Wed Feb 15
21:46:19 GMT 2006
private java.util.Date getDate()
{
return new java.util.Date();
}
Is there a way to pass over the format you want returned. ie
private java.sql.Date getDate()
{
return new java.sql.Date();
}
Thanks
Thomas Fritsch - 17 Feb 2006 11:25 GMT
> I am trying to retrieve the system date in the date format dd/mm/yyyy
> to use in an sql statement.
[quoted text clipped - 11 lines]
> return new java.sql.Date();
> }
I'm not sure I understood you correctly. You want an java.sql.Date
representing the current date/time ?
return new java.sql.Date((new java.util.Date()).getTime());
will do this.

Signature
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Rob Skedgell - 17 Feb 2006 11:30 GMT
> Newbie
>
[quoted text clipped - 13 lines]
> return new java.sql.Date();
> }
You can get a java.sql.Date from a java.util.Date with:
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
where utilDate contains a java.util.Date instance and the getTime()
method returns the number of milliseconds since the epoch (January 1,
1970, 00:00:00 GMT). The SQL date constructor which takes a long "will
set the time components to the time in the default time zone (the time
zone of the Java virtual machine running the application) that
corresponds to zero GMT." (from the 1.5.0_06 javadocs)
For the current date,
java.sql.Date today = new java.sql.Date((new java.util.Date()).getTime);

Signature
Rob Skedgell <rob+news@nephelococcygia.demon.co.uk>
GnuPG/PGP: 7DA3 1579 C0DD 8748 C05A B984 E2A2 3234 D14B 6DD7
Rob Skedgell - 17 Feb 2006 12:16 GMT
<posted & mailed>
> For the current date,
> java.sql.Date today = new java.sql.Date((new
> java.util.Date()).getTime);
^^^^^^^ getTime() - sorry

Signature
Rob Skedgell <rob+news@nephelococcygia.demon.co.uk>
GnuPG/PGP: 7DA3 1579 C0DD 8748 C05A B984 E2A2 3234 D14B 6DD7
Stefan Waldmann - 17 Feb 2006 13:08 GMT
Hi!
> The function i am using is returning a date format of: Wed Feb 15
> 21:46:19 GMT 2006
[quoted text clipped - 8 lines]
> return new java.sql.Date();
> }
No matter if you have got an object of type java.sql.Date or
java.util.Date - the internal representation of these data types is the
number of milliseconds passed since January 1st, 1970.
"Wed Feb 15 21:46:19 GMT 2006" is how the date is formatted by default
when transformed into a String, which happens e.g. if you do the following:
System.out.println(new java.util.Date());
If you want to print your date in some special, self defined format, use
java.text.SimpleDateFormat:
SimpleDateFormat dFormat = new SimpleDateFormat("dd/mm/yyyy");
System.out.println(dFormat.format(new java.util.Date()));
HTH
Stefan