• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Timestamp assistance

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ricky Jay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I posted it in Servlets, because I'm using this code in a servlet.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll give that a shot.

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

reply
    Bookmark Topic Watch Topic
  • New Topic