'Get multiple times current time on single Button

I have a button showing text START when I click on it I shows me current time in textview.(I consider this time as start time). Than convert the text on Button into STOP. When I click on it again show me current time on another textview(I consider this time as stop time). And then calculate the difference between these two time. I want this same working for multiple time. I means First button show Text ENTER. When I click on it it should show current time in textview.(I will consider this time as Enter time). Than convert the text on Button into START. When I click on it again show me current time on another textview(I will consider this time as start time). Than convert the text on Button into STOP. When I click on it again show me current time on another textview(I will consider this time as STOP time). And last Than convert the text on Button into EXIT. When I click on it again show me current time on another textview(I will consider this time as Exit time).

Here is my code:

 btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isStarted){
                    Calendar calendar = Calendar.getInstance();
                    SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
                    String time = format.format(calendar.getTime());


                    tvend.setText(time);
                    try {
                        Date date1 = format.parse(tvend.getText().toString());
                        Date date2 = format.parse(tvstarttime.getText().toString());
                        long mills = date1.getTime() - date2.getTime();
                        Log.v("Data1", ""+date1.getTime());
                        Log.v("Data2", ""+date2.getTime());
                        int hours = (int) (mills/(1000 * 60 * 60));
                        int mins = (int) (mills % (1000*60*60));

                        String diff = hours + ":" + mins;
                        diffence.setText(diff);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    btn_start.setText("START");
                }else {
                    Calendar calendar = Calendar.getInstance();
                    SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
                    String time1 = format.format(calendar.getTime());


                    tvstarttime.setText(time1);
                    btn_start.setText("STOP");
                }
                isStarted = !isStarted;



            }
        });





Solution 1:[1]

tl;dr

Duration
.between( 
    instant , 
    Instant.now() 
)
.toHoursPart()

Details

You Question is unclear. It it seems like you want to track elapsed time.

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310. Never use Date, Calendar, SimpleDateFormat, etc.

Capture the current moment as seen with an offset from UTC of zero hours-minutes-seconds by using Instant class.

Instant instant = Instant.now() ;

Calculate elapsed time using Duration class.

Duration d = Duration.between( instant , Instant.now() ) ;

Generate text in standard ISO 8601 format.

String output = d.toString() ;

PT2M36S

Interrogate for the parts. Let java.time do the math for you.

int minutes = d.toMinutesPart() ;
int hours = d.toHoursPart() ;

These classes are built into Java 8 and later. Also found on Android 26+. For earlier Android, the latest tooling brings most of the functionality via “API desugaring”.

All of this has been covered many many times in Stack Overflow. Search to learn more.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1