0

enter image description here

this is the screenshot of my application the Time in which is 00:57:18 and the timeout 14:14:21 should be automatically calculated the time difference and the result will show in Total travel hours (circled with red)

What do you think I need to change in my codings to make the output show properly in Total Hours Difference Thank you so much.

this is the code of mine.

FormTimeOut.java

    String date_n = new SimpleDateFormat("MM/dd/yyyy", 
Locale.getDefault()).format(new Date());
    TextView date  = (TextView) findViewById(R.id.editText_timei_timedate);
    date.setText(date_n);


    DisplayDateTime = (TextView) findViewById(R.id.editText_timei_out);
    DisplayDateTimeStart = (TextView) 
findViewById(R.id.editText_travel_start);
    DisplayDateTimeTotal = (TextView) 
findViewById(R.id.editText_travel_total);


    BTN = (Button) findViewById(R.id.button1);

    calander = Calendar.getInstance();
    simpledateformat = new SimpleDateFormat("HH:mm:ss");
    Date = simpledateformat.format(calander.getTime());
    simpledateformat2 = new SimpleDateFormat("HH:mm:ss");
    Date2 = simpledateformat2.format(calander.getTime());

    BTN.setOnClickListener(new View.OnClickListener()

    {

        @Override
        public void onClick (View v){

            DisplayDateTime.setText(Date);
            DisplayDateTimeStart.setText(Date2);

            try {

                Date oldDate = (java.util.Date) 
 simpledateformat2.parse(String.valueOf(simpledateformat));


                Date currentDate = new Date();

                long diff = currentDate.getTime() - oldDate.getTime();
                long seconds = diff / 1000;
                long minutes = seconds / 60;
                long hours = minutes / 60;

                if (oldDate.before(currentDate)) {

                    DisplayDateTimeTotal.setText("hours:" + hours + "minutes:" + minutes);

                }

                // Log.e("toyBornTime", "" + toyBornTime);

            } catch (ParseException e) {

                e.printStackTrace();
            }


        }
    });
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with and also has much better support for calculating the difference between two times. – Ole V.V. Nov 16 '19 at 08:25
  • Possible duplicate of [Time difference between two times](https://stackoverflow.com/questions/15360123/time-difference-between-two-times) – Ole V.V. Nov 16 '19 at 08:27
  • It’s a lot of code for a Stack Overflow question. Please train [how to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Ole V.V. Nov 16 '19 at 08:28
  • 1
    @OleV.V.okay I will edit my question to minimize the codings so the others would not be confused. – Jullian Arnold Nov 16 '19 at 08:35
  • Actually im so down right now I cant find the solution for this one, you know that I successfully show the calculation in textview but it is not correct Im getting wrong answers – Jullian Arnold Nov 16 '19 at 08:37
  • @OleV.V. I already edited my codes sir. – Jullian Arnold Nov 16 '19 at 08:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202456/discussion-between-jullian-arnold-and-ole-v-v). – Jullian Arnold Nov 16 '19 at 08:42

2 Answers2

1

I think what you need is to show the time difference in the below text view. Hope the below code helps to clear your doubt.

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

date1 = simpleDateFormat.parse("00:57:18");
date2 = simpleDateFormat.parse("14:14:21");

long difference = date2.getTime() - date1.getTime();
hours = (int) ((difference - (1000*60*60)); 
min = (int) (difference - (1000*60*60*hours)) / (1000*60);
sec = (int) (difference - (1000*60*60*hours)) - (1000*60*min)) / (1000);
hours = (hours < 0 ? -hours : hours);
String totalTime = String.valueOf(hours) + ":" + String.valueOf(min) + ":" + String.valueOf(sec);
DisplayDateTimeTotal.setText(totalTime);
Midhun Murali
  • 921
  • 6
  • 11
0

For time calculate I have used this :

long difference = date2.getTime() - date1.getTime(); 
days = (int) (difference / (1000*60*60*24));  
hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60)); 
min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);

you can convert your text into Date object by using the following code

  DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
 Date dateObject;

try{
String dob=(tx.getText().toString());
dateObject = formatter.parse(dob);
date = new SimpleDateFormat("dd/MM/yyyy").format(dateObject);

}
bugfreerammohan
  • 1,471
  • 1
  • 7
  • 22