Hai to all, While I doing my project i face a problem of getting the system time and store it into the SQL Server database, so i try to use the date class of util and also with the sql server, but only format finally i able to insert is yyyy-mm-dd, so if anyone will tell me how to save it in the normal mm-dd-yyyy format or else get the system date and directly store it into the sqlserver ...
Thanks in advance
Regards
M. Tamilselvan
Thursday, September 4, 2008
Subscribe to:
Post Comments (Atom)
1 comment:
Hi Tamilselvan,
If you are trying to do it using Java then
You can store the current Date to the column of the table using
java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
preparedstatement.setDate(n,date);
(You need to be thinking of doing this from the Middle Tier and not from Client Machine as the Client usually has the freedom to change the System Date and or Time)
Instead if it is a custom date that has been entered use the below code
Calendar calendar = new GregorianCalendar(2001, 0, 1);
java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
or a much simpler code
java.sql.Date jsqlD = java.sql.Date.valueOf( "2010-01-31" );
IT MAY BE BETTER TO HAVE THE DATABASE SERVER DEAL WITH THE DATE AND TIME.
use getDate() function in SQL Server of sysdate in Oracle to fetch the Date Time on the Database Server and Insert or Update the same on to the table.
connection.prepareStatement("Insert into table1 values(getDate())");
You should be able to find more and better way of doing the same.
Happy Knowledge Sharing... :)
Post a Comment