【发布时间】:2019-09-24 04:52:20
【问题描述】:
我正在尝试将 System.currentTimeMillis 转换为 (hh:mm:ss) 的当前时间格式。到目前为止,这是我尝试过的方法,但效果不佳。
Long currentTime = System.currentTimeMillis();
int hours;
int minutes;
int seconds;
String getSecToStr = currentTime.toString();
String getTimeStr = getSecToStr.substring(8,13);
seconds = Integer.parseInt(getTimeStr);
minutes = seconds / 60;
seconds -= minutes * 60;
hours = minutes / 60;
minutes -= hours * 60;
String myResult = Integer.toString(hours) + ":" + Integer.toString(minutes) + ":" + Integer.toString(seconds);
System.out.println("Current Time Is: " + myResult);
有什么想法吗?非常感谢!
【问题讨论】:
-
怎么不工作了?它与您的预期有何不同?
-
System.currentTimeMillis 拉出一个 13 位数的数字。我相信这些数字包括当前日期和时间。我相信前 8 个数字是日期,后 5 个数字是时间。当我使用 String.substring 分配数字字符 8 到 13 作为我的秒数时,最终结果如下...... 27:13:12 或 5:5:4 这不是我想要的。我想要实际的时间。
-
不是这样的。看看here