简体   繁体   English

Java中两次之间的区别

[英]difference between two times in java

I have searched and found a lot of different things but none that actually help to get what I am looking for. 我进行了搜索,发现了很多不同的东西,但是没有任何东西真正有助于获得我想要的东西。 I have two JComboBoxes in which the user can select different times. 我有两个JComboBoxes,用户可以在其中选择不同的时间。 For example lets say 8:00, and 17:30. 例如,假设8:00和17:30。 Now I want to be able to display the difference between those times, so in this case it would be 9.5. 现在,我希望能够显示这些时间之间的时差,因此在本例中为9.5。 I want it to be in the 9.5, and not 9:30. 我希望它位于9.5,而不是9:30。 But my code is doing 9.3, because it is just converting my string to a double. 但是我的代码正在执行9.3,因为它只是将我的字符串转换为double。

Any help would be great 任何帮助都会很棒

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

You could split the hh:mm string in hours / minutes: 您可以将hh:mm字符串拆分为小时/分钟:

String[] time = sunB.split(":");
int hours = Integer.parseInt(time[0]);
int minutes = Integer.parseInt(time[1]);

double decimalTime = hours + minutes / 60.0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM