I'm having trouble understanding getting timestamps, subtracting them, and being able to assign and use the difference in future calculations. I am using Java in Android Studio.
When I use SimpleDateFormat
and Calendar
, I'm able to get the current time in HH:mm:ss
but I'm not sure how to subtract two of those timestamps from each other and convert the difference into seconds. My end goal is to take the difference, convert it into seconds, assign it to an int, and later compare the value to other, predetermined values. This is a project in Android Studio so I have buttons already set up to fetch the timestamps.
Example:
- Get a current timestamp, say 16:50:32
- Get a second timestamp in the near future, say 16:53:29 (roughly three minutes later)
- Subtract those two timestamps and store the value in seconds, so the final value would be 177 (seconds)
- have this value of 177 in a variable so that I can do more things with it.
I'm able to use the following code to get the timestamps
Calendar calendar = Calendar.getInstance();
SimpleDateFormat startTime = new SimpleDateFormat("HH:mm:ss");
timeTest = startTime.format(calendar.getTime());
SimpleDateFormat endTime = new SimpleDateFormat("HH:mm:ss");
timeTest2 = endTime.format(calendar.getTime());
But after getting two timestamps I do not know how to subtract them, convert them to seconds and store them in an int value.