作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个日期,一个是 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/
我是一名优秀的程序员,十分优秀!