我有两个字符串变量,如开始时间和结束时间。我需要减去结束时间与开始时间计算对于totalTime。
I have two string variables such as StartTime and EndTime. I need to Calculate the TotalTime by subtracting the EndTime with StartTime.
开始时间和结束时间的格式类似如下:
The Format of StartTime and EndTime is as like follows:
StartTime = "08:00 AM";
EndTime = "04:00 PM";
TotalTime在小时和分钟]格式。如何计算这在Android中?
TotalTime in Hours and Mins Format. How to calculate this in Android?
尝试下面code。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
date1 = simpleDateFormat.parse("08:00 AM");
date2 = simpleDateFormat.parse("04:00 PM");
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);
hours = (hours < 0 ? -hours : hours);
Log.i("======= Hours"," :: "+hours);
输出 - 开放时间:8
Output - Hours :: 8