Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get timestamp and convert it to string

Tags:

java

I'm using this to get current timestamp in seconds and add it to a string Double.toString((System.currentTimeMillis()/1000)) However instead of decimal notation I get "1.23213E9". How do I switch to the decimal notation ?

like image 357
user1462299 Avatar asked Jul 18 '12 08:07

user1462299


3 Answers

The shortest is

String secs = "" + System.currentTimeMillis() / 1000;

If you want to retain milli-seconds you can use

String secs = String.format("%.3f",  System.currentTimeMillis() / 1000.0);

produces a String like

1342604140.503
like image 185
Peter Lawrey Avatar answered Nov 15 '22 01:11

Peter Lawrey


Try this:

TimeUnit.MILLISECONDS.toSeconds(((System.currentTimeMillis());
like image 34
Viktor Mellgren Avatar answered Nov 14 '22 23:11

Viktor Mellgren


String.valueOf(System.currentTimeMillis() / 1000)

that should do the trick? No need to convert it to a double

like image 28
tom Avatar answered Nov 15 '22 00:11

tom