gpt4 book ai didi

android - 如何以天时分秒的形式从两个日期中获取差异

转载 作者:行者123 更新时间:2023-11-29 17:46:07 25 4
gpt4 key购买 nike

我有两个日期,一个是 future 日期,另一个是当前日期。我想知道这两个日期之间的区别,我正在使用以下代码,但输出不是我想要的,请帮助我解决这个问题。我是 android 编程的新手。

public void getDateDifference(String FutureDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

try {

Date currentDate = dateFormat.parse(FutureDate);
System.out.println(currentDate);

Date oldDate = new Date();

long diff = currentDate.getTime() - oldDate.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;

counterDaysTV.setText(days + "");
counterMinsTV.setText(minutes + "");
counterHoursTV.setText(hours + "");
counterSecTV.setText(seconds + "");


} catch (Exception e) {

e.printStackTrace();
}

}

我正在使用这种方法来找出差异,但它显示 71 天是正确的,但接下来它显示 1716 小时 102987 分钟 6179267 秒我希望它显示为倒计时,请帮助我解决这个问题。

最佳答案

你总是用整个时间差来计算。

您首先必须计算天数,然后计算剩余的小时数。然后剩下的就是 session 记录等等。

public void getDateDifference(String FutureDate) {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

try {
Date currentDate = dateFormat.parse(FutureDate);
System.out.println(currentDate);

Date oldDate = new Date();

long diff = currentDate.getTime() - oldDate.getTime();

long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);

long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);

long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);

long seconds = diff / 1000;

counterDaysTV.setText(days + "");
counterMinsTV.setText(minutes + "");
counterHoursTV.setText(hours + "");
counterSecTV.setText(seconds + "");

} catch (Exception e) {
e.printStackTrace();
}
}

关于android - 如何以天时分秒的形式从两个日期中获取差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26570281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com