Java DateFormat.format返回01:00:00,格式为HH:mm:ss
我正在使用Java DateFormat.format返回01:00:00,格式为HH:mm:ss,java,android,time,date-format,simpledateformat,Java,Android,Time,Date Format,Simpledateformat,我正在使用android.text.format.DateFormat将长时间转换为小时、分钟和秒。以下是我正在测试的代码: DateFormat.format("HH:mm:ss", 1000) 这将返回: 01:00:01 为什么会这样?根据引用,它应该返回00:00:01,但它不返回。我尝试了所有的组合,但仍然没有区别。如果DateFormat不起作用,我可能不得不恢复使用TimeUnit.millizes.to…方法。DateFormat观察您的本地时区。如果我理解你的问题,那么你需
android.text.format.DateFormat
将长时间转换为小时、分钟和秒。以下是我正在测试的代码:
DateFormat.format("HH:mm:ss", 1000)
这将返回:
01:00:01
为什么会这样?根据引用,它应该返回
00:00:01
,但它不返回。我尝试了所有的组合,但仍然没有区别。如果DateFormat不起作用,我可能不得不恢复使用TimeUnit.millizes.to…
方法。DateFormat观察您的本地时区。如果我理解你的问题,那么你需要
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String str = df.format(1000);
System.out.println(str);
输出为(如预期的那样)
请注意,如果您真的要这样做,那么日期不是持续时间或间隔。您将隐含地使用英国时间作为时区为GMT+1的日期1970年1月1日(历元)。谢谢!最后,我使用了一种与您使用日历实例略有不同的方法,将时区设置为UTC,设置时间,并使用DateFormat,就像之前使用日历实例一样。
00:00:01