Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can i insert current date and time to db

 
Mullin Yu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i use the following code, but the DateTime field at mysql is
2005-04-08 00:00:00

i want to have the time too. where should i change

..
..
// Get the system date and time.
java.util.Date utilDate = new Date();
// Convert it to java.sql.Date
java.sql.Date date = new java.sql.Date(utilDate.getTime());
..
..
..
PreparedStatement stmt = connection.prepareStatement(sql); stmt.setDate(1, date);
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using java.util.GregorianCalendar
Bili
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even i had the same proble..where the time got converted to 0's...

and date remained fine..

What i did was used util.date for dates instead of sql.date

and while updating the db converted that into string of the format

"yyyy-MM-dd HH:mm:ss"

And it worked fine for me..

Even though we should have been using sql.date but i could nt get it working..I know it was a turnaround..but

 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
User java.sql.Timestamp instead of java.sql.Date. java.sql.Date does not carry time information (as per the SQL specification!); java.sql.Timestamp does.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel McNary:
User java.sql.Timestamp instead of java.sql.Date. java.sql.Date does not carry time information (as per the SQL specification!); java.sql.Timestamp does.

Indeed.

It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of documentation that you can even download for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.

Java� API Specifications
Java� 1.5 JDK Javadocs
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, depending on the database you're using, you might consider setting up your table so that it uses a timestamp field, or so your date field otherwise has the current date/time as the default value.

Then you don't have to worry about the current date/time in your Java code. You simply insert the new record, either passing a "null", or just not giving it any data for that field. The database engine will automatically use the current date/time when it adds the record.

Although, again, whether you can do this, and how you would do it, all depends on your back-end database.

- Jeff
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JDBC...
 
M Burke
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also insert directly using SQL with SYSDATE
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic