如何在天,小时(24),分钟(60),秒(60)两个日期之间取得差异。
和
我一直在经历
Android difference between Two Dates
How do I get difference between two dates in android?, tried every thing and post
但没有帮助,
这是我的代码..
try {
String FinalDate = "20-04-2018 08:00:00";
String CurrentDate = "26-04-2018 10:10:30";
Date date1;
Date date2;
SimpleDateFormat dates = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
date1 = dates.parse(CurrentDate);
date2 = dates.parse(FinalDate);
/*long difference = date1.getTime() - date2.getTime();
long differenceInMinutes = difference / (60 * 1000);
long differenceInSeconds = difference / 1000;
String strMinuteDifference = Long.toString(differenceInMinutes);
String strSecondsDifference = Long.toString(differenceInSeconds);*/
long difference = date1.getTime() - date2.getTime();
long seconds = difference / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
Log.e("TAG_5", "CurrentDate is : " + date1);
Log.e("TAG_5", "Final date is : " + date2);
Log.e("TAG_5", "Day Difference: " + days);
Log.e("TAG_5", "hours Difference: " + hours);
Log.e("TAG_5", "Minute Difference: " + minutes);
Log.e("TAG_5", "Seconds Difference: " + seconds);
} catch (Exception exception) {
Log.e("TAG_5", "exception " + exception);
}
,输出
E/TAG_5: CurrentDate is : Thu Apr 26 10:10:30 GMT+05:30 2018
E/TAG_5: Demo date is : Fri Apr 20 08:00:00 GMT+05:30 2018
E/TAG_5: Day Difference: 6
E/TAG_5: hours Difference: 146
E/TAG_5: Minute Difference: 8770
E/TAG_5: Seconds Difference: 526230
似乎代码是计算这两个日期之间的所有小时数,分钟数,秒数,但
我希望输出像...... 小时应该是2小时,10小时或23小时但不超过24小时,因为25小时将是新的一天,所以应该是1天1小时。
和分钟类似于10分钟35分钟或59分钟,但不超过60分钟
同样适用于秒,它应该是12秒,40秒或59秒但不超过60秒。 那我该怎么做呢?
答案 0 :(得分:2)
像你说的那样计算“休息”时间。 (所以24小时以下)你可以使用modulo。
在计算中,模运算在除法后找到余数 一个数字与另一个数字(有时称为模数)。
int hours = theAmountOfHours % 24
在你的例子中
Log.e("TAG_5", "Day Difference: " + days);
Log.e("TAG_5", "hours Difference: " + hours % 24);
Log.e("TAG_5", "Minute Difference: " + minutes % 60);
Log.e("TAG_5", "Seconds Difference: " + seconds % 60);
资料来源:Wikipedia
答案 1 :(得分:0)
您可以使用两个日期的纪元时间(unix时间戳),并使用模数(% - 余数)运算符自行计算天数,小时数,分钟数和短信数。
答案 2 :(得分:0)
1. Divide
获取天数/小时/分钟等的天数差异。
2.执行 Modulo
以获取剩余秒数。
使用
long difference="your difference result here";
long seconds =1000;
long minutes =60*seconds;
long hours = 60*minutes;
long days = 24*hours;
long weeks=7*days;
long months=30*days;
long year=365*days;
if(difference>year) {
Log.d("year", String.valueOf(difference / year));
difference = difference % year;
}
if(difference>months) {
Log.d("months", String.valueOf(difference / months));
difference = difference % months;
}
if(difference>weeks) {
Log.d("weeks", String.valueOf(difference / weeks));
difference = difference % weeks;
}
if(difference>days) {
Log.d("days", String.valueOf(difference / days));
difference = difference % days;
}
if(difference>hours) {
Log.d("hours", String.valueOf(difference / hours));
difference = difference % hours;
}
if(difference>minutes) {
Log.d("minutes", String.valueOf(difference / minutes));
difference = difference % minutes;
}
if(difference>0)
Log.d("seconds", String.valueOf(difference/seconds ));
答案 3 :(得分:0)
你可以这样做,我希望它对你有帮助。感谢
String CurrentDate = "26-04-2018 10:10:30";
String FinalDate = "20-04-2018 08:00:00";
long diffInMillisec = CurrentDate.getTime() - FinalDate.getTime();
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMillisec);
seconds = diffInSec % 60;
diffInSec/= 60;
minutes =diffInSec % 60;
diffInSec /= 60;
hours = diffInSec % 24;
diffInSec /= 24;
days = diffInSec;`
答案 4 :(得分:0)
试试这个方法
public void printDifferenceDateForHours(Date startDate, Date endDate) {
//milliseconds
long different = endDate.getTime() - startDate.getTime();
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
//TODO Here you will get the days
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
//TODO Here you will get the hours
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
//TODO Here you will get the minute
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
//TODO Here you will get the second
long elapsedSeconds = different / secondsInMilli;
}
答案 5 :(得分:0)
try {
String FinalDate = "20-04-2018 08:00:00";
String CurrentDate = "26-04-2018 10:10:30";
Date date1;
Date date2;
SimpleDateFormat dates = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
date1 = dates.parse(CurrentDate);
date2 = dates.parse(FinalDate);
long seconds = 1000;
long minutes = 60 * seconds;
long hours = 60 * minutes;
long days = 24 * hours;
long weeks = 7 * days;
long months = 30 * days;
long year = 365 * days;
long difference = date1.getTime() - date2.getTime();
long differenceInDays = difference / days;
difference = difference - (differenceInDays * days);
long differenceInHours = (difference) / hours;
difference = difference - (differenceInHours * hours);
long differenceInMin = (difference) / minutes;
difference = difference - (differenceInMin * minutes);
long differenceInSecond = difference / seconds;
Log.e("TAG_5", "CurrentDate is : " + date1);
Log.e("TAG_5", "Final date is : " + date2);
Log.e("TAG_5", "Day Difference: " + differenceInDays);
Log.e("TAG_5", "hours Difference: " + differenceInHours);
Log.e("TAG_5", "Minute Difference: " + differenceInMin);
Log.e("TAG_5", "Seconds Difference: " + differenceInSecond);
} catch (Exception exception) {
Log.e("TAG_5", "exception " + exception);
}
这里我只计算了日,月,分,秒,你可以用同样的方式计算年,月,周