简体   繁体   中英

Convert Timestamp to date and hour Java

Hi i'm getting a dates and hours in timestamp format from a webservice and i'm trying to convert them in String to show them in my app

i've tried

Timestamp ts = new Timestamp(jsonObject.getLong("release_date"));

or

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");   
Date date = new Date( jsonObject.getLong("release_date"));

and it always give me "1970-01-17 07:54:39.6" even if i get other timestamp

for example both 1407279600 and 1406674800 give me "1970-01-17"

any idea on how to do it ?

It sounds like your timestamp format is probably in seconds since the Unix epoch, instead of the milliseconds that Java expects - so just multiply it by 1000:

Date date = new Date(jsonObject.getLong("release_date") * 1000L);

You should also think about what time zone you're interested in, and set that on your SimpleDateFormat .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM