I created buttons for display current time for start and stop parking and its work.
But when I clicked on the deductbtn, the apps on the phone will pop up and show "my Application 1 has stopped"
How can I fix this? Any help will be much appciated.
@Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_payment_info); firebaseAuth = FirebaseAuth.getInstance(); parkBtn = (Button)findViewById(R.id.parkBtn); stopparkBtn = (Button)findViewById(R.id.stopparkBtn); deductBtn = (Button)findViewById(R.id.deductBtn); timeResult = (TextView)findViewById(R.id.timeResult); timeResult2 = (TextView)findViewById(R.id.timeResult2); diffResult = (TextView)findViewById(R.id.diffResult); parkBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final Date d = new Date(); SimpleDateFormat sdf1=new SimpleDateFormat("hh:mm a"); final String currentDateTimeString = sdf1.format(d); timeResult.setText(currentDateTimeString); } }); stopparkBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final Date d1= new Date(); SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm a"); final String currentDateTimeString1 = sdf2.format(d1); timeResult2.setText(currentDateTimeString1); } }); deductBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { float totalTime = Float.parseFloat(timeResult2.getText().toString()); float totalTime1 = Float.parseFloat(timeResult.getText().toString()); final float timeUsage = totalTime - totalTime1; diffResult.setText(String.valueOf(timeUsage)); } }); } }
Asked
Active
Viewed 291 times
-3

Nor Sakinah
- 49
- 8
-
2Provide the Logcat errors – Abhishek D Apr 26 '18 at 06:02
-
Possible duplicate of [Calculate Difference between two times in Android](https://stackoverflow.com/questions/14110621/calculate-difference-between-two-times-in-android) – AskNilesh Apr 26 '18 at 06:05
-
Thank you, I'll try the code first. – Nor Sakinah Apr 26 '18 at 06:16
-
Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Vladyslav Matviienko Apr 26 '18 at 06:22
-
@NorSakinah kalau ada soalan lagi tanya lah, – Ticherhaz FreePalestine Jul 16 '18 at 13:15
2 Answers
1
The new code is here : @Override public void onClick(View view) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm a");
Date date1 = null;
try {
date1 = simpleDateFormat.parse(timeResult.getText().toString());
} catch (ParseException e) {
e.printStackTrace();
}
Date date2 = null;
try {
date2 = simpleDateFormat.parse(timeResult2.getText().toString());
} catch (ParseException e) {
e.printStackTrace();
}
String diffResult= DateUtils.getRelativeTimeSpanString(date1.getTime(), date2.getTime(), DateUtils.FORMAT_SHOW_TIME).toString();
deductBtn.setText(diffResult);
}

Nor Sakinah
- 49
- 8
0
You are getting exception because
float totalTime = Float.parseFloat(timeResult2.getText().toString());
float totalTime1 = Float.parseFloat(timeResult.getText().toString());
Here, If you click this button after performing click on other buttons, then textview's text will be in the format hh:mm a
And if you click button for the first time without clicking other buttons , The strings will be empty
Both will throw exception
java.lang.NumberFormatException: Invalid float
You can use,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm a");
date1 = simpleDateFormat.parse(timeResult2.getText().toString());
date2 = simpleDateFormat.parse(timeResult.getText().toString());
String diff= DateUtils.getRelativeTimeSpanString(date1.getTime(), date2.getTime(), DateUtils.HOUR_IN_MILLIS).toString();
Make sure that your textviews strings are not empty before doing it.

Jyoti JK
- 2,141
- 1
- 17
- 40
-
I see, since the timeResult is in TextView, I can't just simply substract like this: timeResult2 - timeResult. How can I fix this? – Nor Sakinah Apr 26 '18 at 06:12
-
check #Nilesh Rathod comment. The [link](https://stackoverflow.com/questions/14110621/calculate-difference-between-two-times-in-android) has what you want . And I will edit it in my answer – Jyoti JK Apr 26 '18 at 06:13
-
I used the code that you give and I added try & catch and it works! thank you ^-^ – Nor Sakinah Apr 30 '18 at 15:12