Java:SimpleDataFormat将1添加到天和小时(时间跨度)
我正在尝试以“天:小时:分钟:秒”格式输出“900000毫秒”,我现在正在使用以下代码:Java:SimpleDataFormat将1添加到天和小时(时间跨度),java,time,format,simpledateformat,timespan,Java,Time,Format,Simpledateformat,Timespan,我正在尝试以“天:小时:分钟:秒”格式输出“900000毫秒”,我现在正在使用以下代码: SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:HH:mm:ss"); String formatted = simpleDateFormat.format(900000) 900000毫秒应该是15分钟,所以我希望它返回 00:00:15:00 或者像这样的 但不管出于什么原因,它都会回来 01:01:15:00 有人能告诉我
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:HH:mm:ss");
String formatted = simpleDateFormat.format(900000)
900000毫秒应该是15分钟,所以我希望它返回
00:00:15:00
或者像这样的
但不管出于什么原因,它都会回来
01:01:15:00
有人能告诉我为什么以及如何修理它吗
我认为它必须与时区有关,但它也增加了1天的天数
提前谢谢 日期是01,因为它代表1月1日。小时01可能与您所在的时区有关。 试试这个:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = simpleDateFormat.format(900000);
传递给SimpleDataFormat的值900000是一个时间点。。。1970年1月1日00:00:00 GMT后15分钟或: 1970年1月1日00:15:00GMT
在1970年的这个日期和时间,在你所在的时区,天是1,小时是1。答案是正确的。SimpleDate.format(900000)语句对超过纪元900000毫秒的时间进行格式化,即第一个小时的15分钟如果使用
SimpleDateFormat
SimpleDataFormat
用于获取不计算时间的日期实例
您应该使用时间单位
这里有一个例子
public static void main(String[] args) throws Exception {
long millis = 900000;
String s = String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(s);
}
这会让你
15分钟0秒
只需根据您的心意设置格式并添加天数和小时数…我认为您不能使用
dd
数天,因为一d
是一个月的一天,而不是dd。hd给我的是1而不是01,但不幸的是不是0:(非常感谢,这固定了我的小时数!:)知道如何确定天数吗?