• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to use the Date object

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replace
Date now=new Date(???);
with
Date now=new Date("2002-05-10");
Anup Batra
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u all, i'll try it now!
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Everybody's invited. Even this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic