java time 转 int

来源:百度知道 编辑:UC知道 时间:2024/09/28 09:15:55
代码如下:
Player p= Manager.createPlayer(f.toURI().toURL());//f是file类对象
p.start();
Time tt=p.getMediaTime();
Time tt2=p.getDuration();
int a=Integer.parseInt(tt.toString());
int b=Integer.parseInt(tt2.toString());
s.setValue(a/b*100);//s是JSlider
jtf.setText(f.getAbsolutePath());//jtf是jtextfield

在parseInt处抛出异常。
请问如何将time转为int型?

你说的是要把Time转成毫秒吧,int a=tt.getTime(); int b=tt2.getTime();

我记得Time的toString返回的是hh:mm:ss的格式,所以要转换成int的话,大概这么写吧:
public int timeToInt(Time t){
String[] time=t.toString().split(":");
int hour=Integer.parseInt(time[0]);
int min=Integer.parseInt(time[1]);
int sec=Integer.parseInt(time[2]);
return hour*3600+min*60+sec;
}