java 获取时间戳并将其转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11537467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:29:40  来源:igfitidea点击:

Get timestamp and convert it to string

java

提问by user1462299

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 ?

我正在使用它以秒为单位获取当前时间戳并将其添加到字符串Double.toString((System.currentTimeMillis()/1000))但是我得到的不是十进制表示法而是“1.23213E9”。如何切换到十进制表示法?

回答by Viktor Mellgren

Try this:

试试这个:

TimeUnit.MILLISECONDS.toSeconds(((System.currentTimeMillis());

回答by Peter Lawrey

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

回答by tommo

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

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

这应该够了吧?无需将其转换为双精度

回答by Dario

If you need to deal with a Double you could do something like this:

如果你需要处理一个 Double 你可以做这样的事情:

double myNum = 1.23213E9;
String myString = NumberFormat.getInstance().format(myNum);
System.out.print(myString);