简体   繁体   中英

how to convert timestamp format to date-format in Java?

How to convert timestamp 1470989229.000014 to date format mm-dd-yyyy in java? I could do it for different timestamp formats but I could not parse this one.

The database used is MySQL so the timestamp probably would be in that format.

first question, what programming language did you used?

in case you're using PHP, you can try date("mm-dd-yyyy", (1470989229.000014)) but if you want the result like 08-12-2016 , you can use date("mdY", (1470989229.000014))

The timestamp you have there is an unix timestamp. It is defined as the seconds since 1 January 1970 00:00:00 UTC .

So your timestamp 1470989229.000014 translates to 12 August 2016 08:07:09 UTC .

Edit:

Here is an example how to convert the timestamp in java.

double timestamp = 1470989229.000014;
Date date = new Date(Math.round(timestamp * 1000));
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
System.out.println(dateFormat.format(date));

This converts your timestamp to 08-12-2016

In MySQL you can do this using FROM_UNIXTIME followed by DATE_FORMAT function.

SELECT DATE_FORMAT(FROM_UNIXTIME(1470989229.000014),'%m-%d-%Y')

And the output( mm-dd-yyyy ) would look like below:

08-12-2016

Demonstrated Here

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