如何在android中计算经过的时间和日期
我希望我的应用程序显示从数据库中存储条目到从数据库中获取条目所经过的时间 我目前正在做类似的事情,这给了我以秒为单位的时间:如何在android中计算经过的时间和日期,android,date,android-studio,time,calendar,Android,Date,Android Studio,Time,Calendar,我希望我的应用程序显示从数据库中存储条目到从数据库中获取条目所经过的时间 我目前正在做类似的事情,这给了我以秒为单位的时间: SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss aa"); Date systemDate = Calendar.getInstance().getTime(); String myDate = sdf.forma
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)
然后,您需要确定需要多少天,然后根据该数字计算年龄,以获得剩余的毫秒数
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;
}
可能重复此@DeepakSachdeva,非常感谢。!!这解决了我的问题@是的,有点接近。谢谢!对于这样的应用程序,使用System.currentTimeMillis()或Calendar函数是否更好?这取决于您的需要
System.currentTimeMillis()
将使用UTC时区获取当前时间(在您的设备上设置)。这是不可配置的。在大多数情况下,这很好。使用日历
,您可以添加一定的时间或减去,应用不同的时区等。没问题,如果这对您有帮助,请不要忘记向上投票,并用绿色勾选此作为您的答案。