Search code examples
javaandroidtry-catchtimepicker

Get overall hours from two time picker


What is the best way to get the overall total hours from two time picker? I have two different time picker, each time picker has time in and time out. The coding below works fine if the two time picker has filled but doesn't work if only one time picker is filled. Any suggestions would greatly appreciated.

 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 will sum up the total from editTextH1 and editTextH2...If this two time picker is filled, everything works fine. But now I want to set the editText8 will get the value also even though one time picker is filled (value of editText8==editTextH1 when only one time picker is filled)...PLEASE help..I've been stucking at here more than two days...


Solution

  • If you move the variables to hold the difference into the outer scope, you won't need to try to get them to add them.

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

    I would also use TimeUnit.MILLISECONDS.toMinutes(difference) and TimeUnit.MILLISECONDS.toHours(difference) as was done in How to find the duration of difference between two dates in java?