DateFormat.format以HH:mm:ss格式返回01:00:00

时间:2016-04-23 18:19:49

标签: java android time date-format simpledateformat

我正在使用android.text.format.DateFormat将长时间转换为小时,分钟和秒。这是我正在测试的代码:

DateFormat.format("HH:mm:ss", 1000)

返回:

01:00:01

这是为什么?根据{{​​3}}引用,它应该返回00:00:01,但它不会。我尝试了所有组合,但仍然没有区别。如果DateFormat不能工作,我可能不得不恢复使用TimeUnit.MILLISECONDS.to...方法。

1 个答案:

答案 0 :(得分:2)

DateFormat会观察您当地的时区。如果我理解你的问题,那么你需要类似的东西,

DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String str = df.format(1000);
System.out.println(str);

输出(正如预期的那样)

00:00:01