Sum timestamps from two textviews and display in new textview

Viewed 30

I have two textviews and two buttons. When you click on the first button, a window with a choice of time opens. You select the time and it is displayed in textview1. Similarly with the second button and textview2. Now the main task is to sum this time and when you click on the Sum button, display the sum of the time in TextView3 (textview1 + textview2). any ideas how to implement this? preferably with code

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is First Activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to Second Activity"
        android:id="@+id/btnActTwo"
        android:onClick="onClick">
 
    </Button>
 
    <TextView
        android:id="@+id/viewTime1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Time1" />
 
    <Button
        android:id="@+id/time1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choose Time 1"
        android:onClick="setTime"/>
 
    <TextView
        android:id="@+id/viewTime2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Time2" />
 
    <Button
        android:id="@+id/time2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choose Time 2"
        android:onClick="setTime2"
        />
 
    <TextView
        android:id="@+id/sum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sum" />
 
    <Button
        android:id="@+id/btnSum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SUM"
        android:onClick="onClick2"/>
</LinearLayout>

And Main.java code:

package com.example.myapplication_lab4;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
 
import java.util.Calendar;
 
public class MainActivity extends AppCompatActivity {
TextView viewTime1;
    TextView viewTime2;
    TextView sum;
    Button time1;
    Button time2;
    Button btnSum;
    Calendar dateAndTime=Calendar.getInstance();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        viewTime1 = (TextView) findViewById(R.id.viewTime1);
        viewTime2 = (TextView) findViewById(R.id.viewTime2);
        sum = (TextView) findViewById(R.id.sum);
        time1 = (Button) findViewById(R.id.time1);
        time2 = (Button) findViewById(R.id.time2);
        btnSum = (Button) findViewById(R.id.btnSum);
 
    }
    // display a dialog box for selecting the time for 1
    public void setTime(View v) {
        new TimePickerDialog(MainActivity.this, t,
                dateAndTime.get(Calendar.HOUR_OF_DAY),
                dateAndTime.get(Calendar.MINUTE), true)
                .show();
    }
    // display a dialog box for selecting the time for 2
    public void setTime2(View v) {
        new TimePickerDialog(MainActivity.this, t2,
                dateAndTime.get(Calendar.HOUR_OF_DAY),
                dateAndTime.get(Calendar.MINUTE), true)
                .show();
    }
    // setting start time for 1
  private void setInitialDateTime() {
 
        viewTime1.setText(DateUtils.formatDateTime(this,
                dateAndTime.getTimeInMillis(),
                 DateUtils.FORMAT_SHOW_TIME));
    }
    // setting start time for 2
    private void setInitialDateTime2() {
 
        viewTime2.setText(DateUtils.formatDateTime(this,
                dateAndTime.getTimeInMillis(),
                DateUtils.FORMAT_SHOW_TIME));
    }
    // setting the time picker for 1
    TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            dateAndTime.set(Calendar.MINUTE, minute);
            setInitialDateTime();
        }
    };
    // setting the time picker for 2
    TimePickerDialog.OnTimeSetListener t2 = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            dateAndTime.set(Calendar.MINUTE, minute);
            setInitialDateTime2();
        }
    };
 
 
 
 
//irrelevant to the issue
    //Click to go to Activity 2
    public void onClick(View view) {
        Button btnActTwo;
        btnActTwo = (Button) findViewById(R.id.btnActTwo);
 
        if (view.getId() == R.id.btnActTwo) {//call sec act
            Intent intent = new Intent(this, Activity2.class);
            startActivity(intent);
        }
    }
 
    public void onClick2(View view) {
 
    }
}
1 Answers

Here is sample code of sum two date.

String time1="0:30:32";
String time2="0:35:20";

SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

Date date1 = timeFormat.parse(time1);
Date date2 = timeFormat.parse(time2);

long sum = date1.getTime() + date2.getTime();

String date3 = timeFormat.format(new Date(sum));
Log.e("TAG", "Date sum is => " + date3);

**Output: ** Date sum is => 01:05:52

Related