我希望我的应用程序显示从将条目存储在数据库中到从数据库中提取条目所花费的时间。
我目前正在做这样的事情,这给了我以秒为单位的流逝时间:
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss aa");
Date systemDate = Calendar.getInstance().getTime();
String myDate = sdf.format(systemDate);
Date Date1 = sdf.parse(myDate);
Date Date2 = sdf.parse(savedDate);
int dateDiff = getTimeRemaining();
long millse = Date1.getTime() - Date2.getTime();
long mills = Math.abs(millse);
long Mins = mills/(1000*60);
String diff = +dateDiff+ "days " + Mins+ " mins";
cal[1] = "" +Mins;
t3.setText(diff);
但我还希望它包含自存储数据以来的天数。截至目前,当一天结束时它会重置。我希望它能给出 N 天后的总分钟数。我该怎么做呢?提前致谢。
最佳答案
您首先需要确定从数据库存储时间到现在的秒数。
long ageOfDatabaseEntry = (System.currentTimeMillis() - databaseEnteredTimeMillis)
然后您需要确定您想要多少天,然后 modulo年龄除以该数字以获得剩余的毫秒数。
long getRemainingMinutes(long age, int days) {
// Use the modulus operator to get the remainder of the age and days
long remainder = age % (days * 86400000);
if(remainder == age) {
throw new InvalidArgumentException("The number of days required exceeds the age of the database entry. Handle this properly.");
}
// Turn the remainder in milliseconds into minutes and return it
return remainder / 60000;
}
关于android - 如何在android中计算耗时和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460086/