本文介绍了计算Android中两次之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个字符串变量,例如 StartTime 和 EndTime.我需要通过用 StartTime 减去 EndTime 来计算 TotalTime.
I have two string variables such as StartTime and EndTime. I need to Calculate the TotalTime by subtracting the EndTime with StartTime.
StartTime 和 EndTime 的格式如下:
The Format of StartTime and EndTime is as like follows:
StartTime = "08:00 AM";
EndTime = "04:00 PM";
以小时和分钟格式表示的总时间.如何在 Android 中计算此值?
TotalTime in Hours and Mins Format. How to calculate this in Android?
推荐答案
试试下面的代码.
//假设时间格式为("hh:mm a")格式
// suppose time format is into ("hh:mm a") format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a");
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
这篇关于计算Android中两次之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!