问题描述
我想使用 currentTimeMillis
两次,这样我就可以计算持续时间,但我也想以用户可读的格式显示时间和日期.我遇到了麻烦,因为 currentTimeMillis
对计算很有用,但我看不到内置函数来转换为合适的时间或时间/日期.
I want to use currentTimeMillis
twice so I can calculate a duration but I also want to display Time and Date in user readable format. I'm having trouble as currentTimeMillis
is good for the calculation but I can't see a built in function to convert to nice time or time/date.
我用
android.text.format.DateFormat df = new android.text.format.DateFormat();
df.format("yyyy-MM-dd kk:mm:ss", new java.util.Date());
为了生成合适的时间和日期,我最终想做的是将结果 currentTimeMillis
值显示到 android.text.format.DateFormat df = new android.text 中.format.DateFormat();
for producing nice time and date and what I'd ultimately like to do is show my resulting currentTimeMillis
value into the android.text.format.DateFormat df = new android.text.format.DateFormat();
例如
android.text.format.DateFormat df = currentTimeMillis();
当我尝试时我得到
类型不匹配:无法从 long 转换为 DateFormat
我试过使用一些强制转换,但不知道如何实现.
I've tried to use some casting but can't see how to accomplish this.
推荐答案
它会起作用.
long yourmilliseconds = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
System.out.println(sdf.format(resultdate));
这篇关于如何将 currentTimeMillis 转换为可读的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!