File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Timestamp assistance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Timestamp assistance" Watch "Timestamp assistance" New topic
Author

Timestamp assistance

Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
In PHP, i can just use Now()...is there anything similar in java?

I've search the forum, but I'm not coming up with anything.

I know there is a java.sql.Timestamp class, but can get the time by using getTime();



Now when I try to insert time_stamp in my MySql table, I get nothing. I've define the timestamp column in my table as:
Time_Stamp | timestamp | NO | | CURRENT_TIMESTAMP |

If someone could point me in a better direction, I would be thankful.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets. For more information, please read this.

This post has been moved to a more appropriate forum.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
Sorry, I posted it in Servlets, because I'm using this code in a servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Just because you are doing something in a servlet doesn't make it a question about servlets.

I think that you will need to post a bit more info about what you are doing for anyone to be able to help.
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
I"m trying to insert a timestamp into my table. If I try to let mysql manage the time/timestamp...the time is off. I'm trying to get the timestamp from java and insert that into the table.

If I use this code, I can't insert anything into the table. If I try to insert just FName, LName, and Password...mysql will insert the timestamp for me but it's the wrong time. I don't want the DBMS to manage the timestamp.

Am I doing something wrong?

If I try and return sdateTime to my index page it will print the correct date and time.


Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
update:
If I change my Time_Stamp column in my table to a varchar instead of CURRENT_TIMESTAMP...i'm able to insert it. What is the value of having the column set to the mysql defualt TIMESTAMP? Does this allow you to calculate difference in time and days easier?

thanks
Jeremy Wilkinson
Greenhorn

Joined: Mar 31, 2010
Posts: 13
You will need to convert your date object to a timestamp object before inserting it. For example...

Calendar cal = Calendar.getInstance();
java.util.Date now = new Date(cal.getTimeInMillis());
java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());

It is much easier to compare dates and check differences when using milliseconds. I use this online utility to help me figure out my time conversions
Ricky Jay
Ranch Hand

Joined: Feb 12, 2010
Posts: 62
I'll give that a shot.

Thanks for the link. I think it will be useful.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timestamp assistance
 
Similar Threads
save Date() into mysql Timestamp
On java.sql.Timestamp and java.sql.Date
How do i convert a Date object into a TimeStamp object
Converet from java.sql.Timestamp to java.util.Date
JSTL: formatDate Vs Timestamp