Results 1 to 4 of 4
  1. #1
    Ace
    Ace is offline Member
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    16
    Rep Power
    0

    Default From MySQL's UNIX_TIMESTAMP() value to Date

    Hi, I would like to know how I'm supposed to convert UNIX_TIMESTAMP() value like 1243962183 to normal date format?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: From MySQL's UNIX_TIMESTAMP() value to Date

    How are you getting the value 1243962183? What is a normal date format?

    If the database has a column holding a TIMESTAMP value then I would try obtaining values from a ResultSet using one of the getTimestamp() methods. They all return an instance of java.sql.Timestamp from which you can obtain a string representation:

    * with toString()
    * using a SimpleDateFormat since Timestamp extends Date (but note the comment in the Timestamp API docs about how Timestamp should not be viewed generically as a sort of Date. I guess you'll lose nanoseconds, but it may give you what you want)
    * using String.format() where the format string would involve the $t flags described in the Formatter API docs. I notice that $t does allow for nanoseconds to be shown and also that it can be used with a long value such as your 1243962183.

  3. #3
    Ace
    Ace is offline Member
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    16
    Rep Power
    0

    Default Re: From MySQL's UNIX_TIMESTAMP() value to Date

    Thanks for your answer :)
    The value 1243962183 is kept in the int type field, filled with the UNIX_TIMESTAMP MySQL's function. Normal date format - I think this way: 2015-10-04.

    I have tried with getTimestamp() and it works, but not properly. The date is shifted (instead of 2009-06-02 is 2015-10-04).

  4. #4
    Ace
    Ace is offline Member
    Join Date
    Nov 2011
    Location
    Earth
    Posts
    16
    Rep Power
    0

    Default Re: From MySQL's UNIX_TIMESTAMP() value to Date

    Problem solved: I had to multiply UNIX_TIMESTAMP by 1000 and it's ok now.

Similar Threads

  1. Replies: 5
    Last Post: 02-16-2012, 10:26 AM
  2. Replies: 4
    Last Post: 08-01-2011, 11:29 AM
  3. Compare Mysql date with current date
    By gioarvan in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-24-2011, 06:19 PM
  4. How to insert DATE into mysql using JSP?
    By sivakumar_sakam in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-27-2009, 08:52 PM
  5. Replies: 12
    Last Post: 04-18-2009, 01:58 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •