user590849
user590849

Reputation: 11765

how to convert timestamp to date in android?

i have a time stamp coming in my XML that i put into my database.

The time stamp is in the format of number of seconds gone by since 1970. i want to convert that into a date object.

how do i go about it?

thank you in advance.

Upvotes: 2

Views: 6551

Answers (3)

fransiskapw
fransiskapw

Reputation: 33

It's been awhile since the question been asked, but i just faced the same trouble. And I found out that the trouble is in the variable type. Are you using integer to store the timestamp value? Try using long. It worked for me

Upvotes: 2

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

Date class has special constructor for this:

Date result = new Date(numberOfSec * 1000);

Further you can format your Date object as you like using SimpleDateFormat.

See the doc.

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114767

You can create a Date instance based on the number of milliseconds since Jan 1, 1970. Your value is expressed in seconds, but that a trivial conversion:

 long timestamp = getTimestampInSeconds();  // some megic to get the value
 Date date = new Date(timestamp * 1000);    // convert to milliseconds

Upvotes: 6

Related Questions