Convert Date to time stamp in java

14,600

Call getTime() on your Timestamp object:

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object.

In fact you don't have to create a Timestamp first. user.getCreatedTime().getTime() already gives you the long value which you want.

Share:
14,600
jackyesind
Author by

jackyesind

Updated on June 04, 2022

Comments

  • jackyesind
    jackyesind almost 2 years

    I am trying to convert the date to timestamp

    Thu Jul 04 13:32:51 IST 2013 to timestamp value

    java.sql.Timestamp timeStampDate = new
                       Timestamp(user.getCreatedTime().getTime());
               System.out.println("Today is " +timeStampDate);
    

    but it gives the result as 2013-07-04 13:32:51.0 I need only the long value like 1372924971000.

  • William Morrison
    William Morrison almost 11 years
    No. He's looking for the elapsed time since the epoch for when the user was created.
  • lalitm
    lalitm almost 10 years
    Just to clarify, Date#getTime() returns this Date as a millisecond value. The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.