1

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...

Hoo
  • 1,806
  • 7
  • 33
  • 66
  • What exactly is wrong with the code here? What would you expect to happen if only one time picker is filled? – Steve Clanton Oct 01 '15 at 02:52
  • @SteveClanton If only one time picker is filled, the editText will get the total hours from the time picker (time in and time out) only, which mean the value should same with editTextH1....If two then it will get from two time picker, – Hoo Oct 01 '15 at 03:04
  • @SteveClanton hv u get what I meant? – Hoo Oct 01 '15 at 03:23
  • Yes. Are you saying editTextH1, editTextH2, and editText8 are all set correctly when both are filled out? Are you saying that none are when it's only one or is the problem only editText8 is not set but editTextH1 (or 2) is? The clearer you make your question, the more likely you are to get an answer. Have you stepped through in the debugger? Is there something that is not doing what you would expect it to do? – Steve Clanton Oct 01 '15 at 03:26
  • It should be like this..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) – Hoo Oct 01 '15 at 03:33

1 Answers1

1

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?

Community
  • 1
  • 1
Steve Clanton
  • 4,064
  • 3
  • 32
  • 38