aspose file tools*
The moose likes JDBC and the fly likes How to use the Date object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to use the Date object" Watch "How to use the Date object" New topic
Author

How to use the Date object

Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
My codes is very simple:
package callcenter;
import java.sql.*;
....
try {
String createRow= "insert into cs_applyform(id,declaretime,declarer,declarerid,troubletype,scenaro,location,worknumber) values(?,?,?,?,?,?,?,?)";
PreparedStatement pstmt=dd.conn.prepareStatement(createRow);
pstmt.setInt(1,newId);
Date now=new Date(???);
pstmt.setDate(2,now);
.......
return true ;
}
catch (Exception r) {
System.out.println("Creation failed!");
return false ;
}
I don't know what should fill in the constraction method of the object Date, i want the variable now contains the current date.
anybody help me?
Irene Loos
Ranch Hand

Joined: Apr 15, 2002
Posts: 78
You can use
java.sql.Date currentDate = new java.sql.Date(new java.util.Date().getTime());
String now = currentDate.toString();
Try it.
Irene


Irene Loos
Anup Batra
Ranch Hand

Joined: Jan 21, 2002
Posts: 41
Replace
Date now=new Date(???);
with
Date now=new Date("2002-05-10");
Anup Batra
Craig Demyanovich
Ranch Hand

Joined: Sep 25, 2000
Posts: 173
Have a look at System.currentTimeMillis() for the parameter to the Date constructor. Be sure to read the documentation mentioned in the JavaDoc for this method concerning computer time and UTC.
Craig
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
Thank u all, i'll try it now!
fei long
Ranch Hand

Joined: Apr 04, 2002
Posts: 48
you can also use '(select sysdate from dual)' at the ? in preparedstatement. like:
insert into tabname values(?,?,(select sysdate from dual),?,?)
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
the answer is:
Date currentDate=new Date(System.currentTimeMillis());
thanks to Craig Demyanovich!
Sean Li
Ranch Hand

Joined: Feb 27, 2002
Posts: 154
But when i took a look of the data stored in the database, i found only the date information was set and the time information is all zero like this: 00-00-00 2002-5-12. my question is how to set the time information. please help me again.
Craig Demyanovich
Ranch Hand

Joined: Sep 25, 2000
Posts: 173
Ordin,
I've never considered the approach that you mentioned, but it's worth having as an option. One disadvantage of using it is that it is database dependent. If your system uses Data Access Objects, though, you have already nicely layered and encapsulated database-specific code; hence, your approach could work very well with Data Access Objects.
Thanks,
Craig
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use the Date object