开发者

Error while converting the value of getTime() to milliseconds

开发者 https://www.devze.com 2023-03-27 04:17 出处:网络
Can you please help in the below matter: I am having an error in the display is that when I am converting to milliseconds it will show wrong values. Iam using getTime() in Java.

Can you please help in the below matter:

I am having an error in the display is that when I am converting to milliseconds it will show wrong values. Iam using getTime() in Java.

My code is:

Time absent = rs.getT开发者_如何学Cime("abs_hours");
long absent_cumm= (absent.getTime() );

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

But when I am printing absent_cumm

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

How to convert the absent value into milliseconds?


Well, here's at least one way that could work:

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);

Should correctly print "ms: 240000"

0

精彩评论

暂无评论...
验证码 换一张
取 消