rde*_*ish 1 java date jcombobox
我搜索并发现了很多不同的东西,但没有一个真正有助于得到我想要的东西.我有两个JComboBox,用户可以在其中选择不同的时间.比如说8:00和17:30.现在我希望能够显示这些时间之间的差异,所以在这种情况下它将是9.5.我希望它在9.5,而不是9:30.但我的代码是9.3,因为它只是将我的字符串转换为double.
任何帮助都会很棒
public void displaytotal() {
Object sunBobj, sunEobj, mon, tues, wed, thur, fri, sat;
double totalD;
sunBobj = jComboBox1.getSelectedItem();
sunEobj = jComboBox2.getSelectedItem();
String sunB = sunBobj.toString();
String sunE = sunEobj.toString();
try {
double sundB = Double.parseDouble(sunB.replace(":", "."));
double sundE = Double.parseDouble(sunE.replace(":", "."));
totalD = ((sundE - sundB)) * 24;
String totalS = "" + totalD;
jLabel17.setText(totalS);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以hh:mm用小时/分钟分割字符串:
String[] time = sunB.split(":");
int hours = Integer.parseInt(time[0]);
int minutes = Integer.parseInt(time[1]);
double decimalTime = hours + minutes / 60.0;
Run Code Online (Sandbox Code Playgroud)