Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use the Date object

 
Sean Li
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use
java.sql.Date currentDate = new java.sql.Date(new java.util.Date().getTime());
String now = currentDate.toString();
Try it.
Irene
 
Anup Batra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace
Date now=new Date(???);
with
Date now=new Date("2002-05-10");
Anup Batra
 
Craig Demyanovich
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u all, i'll try it now!
 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is:
Date currentDate=new Date(System.currentTimeMillis());
thanks to Craig Demyanovich!
 
Sean Li
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic