将getTime()的值转换为毫秒时出错

时间:2011-08-10 09:08:31

标签: java datetime

请你帮忙解决以下问题:

我在显示器中出现错误,当我转换为毫秒时,它将显示错误的值。我在Java中使用getTime()。

我的代码是:

Time absent = rs.getTime("abs_hours");
long absent_cumm= (absent.getTime() );

out.println(absent); // This will display 00:04:00 which is correct

但是当我打印absent_cumm时

out.println(absent_cumm);// This will display -10560000 which is wrong

如何将缺席值转换为毫秒?

1 个答案:

答案 0 :(得分:2)

嗯,至少有一种方法可行:

Time time = Time.valueOf("00:04:00"); // replace with rs.getTime()
Calendar cal = Calendar.getInstance();
cal.setTime(time);
int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int ms = (hour * 3600 + minute * 60 + second) * 1000;
System.out.println("ms: " + ms);

应正确打印“ms:240000”