从两次选择器中获取总小时数

Get overall hours from two time picker

从两个时间选择器获取总体总小时数的最佳方法是什么?我有两个不同的时间选择器,每个时间选择器都有时间和超时。如果两次选择器已填充,下面的代码可以正常工作,但如果只有一次选择器已填充,则无法使用。任何建议将不胜感激。

 SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            Date dateb = null; //time in
            Date datec = null; //time out
            Date dateb1 = null; //time in1
            Date datec1 = null; // time out2

 try {
                dateb = format.parse(b);
                datec = format.parse(c);
                long difference = datec.getTime() - dateb.getTime();
                int minutes = (int) ((difference / (1000 * 60)) % 60);
                int hours = (int) ((difference / (1000 * 60 * 60)) % 24) - 1;
                editTextH1.setText((hours + ":" + minutes));
            } catch (Exception e) {

                System.err.println("ouch!");
            }
            try {

                dateb1 = format.parse(d);
                datec1 = format.parse(e1);
                long difference1 = datec1.getTime() - dateb1.getTime();
                int minutes1 = (int) ((difference1 / (1000 * 60)) % 60);
                int hours1 = (int) ((difference1 / (1000 * 60 * 60)) % 24) - 1;
                editTextH2.setText((hours1 + ":" + minutes1));
            } catch (Exception e) {

                System.err.println("ouch!");
            }

 try {

                long dateb_sum = dateb.getTime() + dateb1.getTime();
                long datec_sum = datec.getTime() + datec1.getTime();
                long difference4 = datec_sum - dateb_sum;
                int minutes4 = (int) ((difference4 / (1000 * 60)) % 60);
                int hours4 = (int) ((difference4 / (1000 * 60 * 60)) % 24) - 1;
                editText8.setText((hours4 + ":" + minutes4));
            }catch(Exception e)
            {
                System.err.println("ouch!");
            }

editText8 将对 editTextH1 和 editTextH2 的总数求和...如果这两次选择器被填充,一切正常。但现在我想设置 editText8 也将获得值,即使一次选择器被填充(当只有一次选择器被填充时 editText8==editTextH1 的值)......请帮助......我一直在这里停留更多两天以上...

如果将变量移动到外部范围以保持差异,则无需尝试让它们添加它们。

SimpleDateFormat format = new SimpleDateFormat("HH:mm");
long difference = 0;
long difference1 = 0;
try {
    Date dateb = format.parse(b); //time in
    Date datec = format.parse(c); //time out
    difference = datec.getTime() - dateb.getTime();
    int minutes = (int) ((difference / (1000 * 60)) % 60);
    int hours = (int) ((difference / (1000 * 60 * 60)) % 24) - 1;
    editTextH1.setText((hours + ":" + minutes));
} catch (Exception e) {
     System.err.println("ouch!");
}
try {
    Date dateb1 = format.parse(d);
    Date datec1 = format.parse(e1);
    long difference1 = datec1.getTime() - dateb1.getTime();
    int minutes1 = (int) ((difference1 / (1000 * 60)) % 60);
    int hours1 = (int) ((difference1 / (1000 * 60 * 60)) % 24) - 1;
    editTextH2.setText((hours1 + ":" + minutes1));
 } catch (Exception e) {
    System.err.println("ouch!");
 }
long difference4 = difference + difference1;
int minutes4 = (int) ((difference4 / (1000 * 60)) % 60);
int hours4 = (int) ((difference4 / (1000 * 60 * 60)) % 24) - 1;
editText8.setText((hours4 + ":" + minutes4));

我也会像 How to find the duration of difference between two dates in java?

中那样使用 TimeUnit.MILLISECONDS.toMinutes(difference)TimeUnit.MILLISECONDS.toHours(difference)