[英]Date difference timer of current and past date in android
I am trying to make a difference between current and past date which I am getting perfectly. 我正在努力使当前日期和过去日期有所不同,而现在我已经很完美了。 But what I want is with the timer like if my Current time is 1:02 and my past time is 12:02 then my timer should give me a result like: 01:02:02, 01:00:03, 01:00:03,01:00:04.
但是我想要的是计时器,如果我的当前时间是1:02,过去的时间是12:02,那么计时器应该给我这样的结果:01:02:02,01:00:03,01:00 :03,01:00:04。 I know its not that much complicated problem but I am not able to find a solution for it.
我知道这不是那么复杂的问题,但是我无法找到解决方案。 Right now code which I am working is given below:
现在我正在工作的代码如下:
try {
Date currentSystemClockTime = new Date(System.currentTimeMillis());
long diff = currentSystemClockTime() - serverDateTime.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
txtTimer.setText(days+"D:"+hours+":"+minutes+":"+seconds+"");
} catch (Exception e) {
e.printStackTrace();
}
Thanks in advance 提前致谢
I have solved my problem, I am putting an answer here for other people information. 我已经解决了我的问题,我在这里为其他人提供答案。 I made a method which will calculate date difference in Days, Hours, Minutes and Seconds along with the timer.
我制作了一种方法,可以与计时器一起计算以天,小时,分钟和秒为单位的日期差。
Method for calculating Date Difference: 日期差的计算方法:
public void checkingCondition(){
if(ParseUser.getCurrentUser()!=null && ParseUser.getCurrentUser().has("scanTime")){
final Date scanDateTime = ParseUser.getCurrentUser().getDate("scanTime");
Date now = new Date(System.currentTimeMillis());
try {
long diff = now.getTime() - scanDateTime.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
if(diffDays == 0){
String hours = "", minutes = "", seconds = "";
if(diffHours < 10){
hours = "0"+diffHours;
}else{
hours = ""+diffHours;
}
if(diffMinutes < 10){
minutes = "0"+diffMinutes;
}else{
minutes = ""+diffMinutes;
}
if(diffSeconds < 10){
seconds = "0"+diffSeconds;
}else{
seconds = ""+diffSeconds;
}
txtTimer.setText(hours+":"+minutes+":"+seconds);
}else{
String hours = "", minutes = "", seconds = "";
if(diffHours < 10){
hours = "0"+diffHours;
}else{
hours = ""+diffHours;
}
if(diffMinutes < 10){
minutes = "0"+diffMinutes;
}else{
minutes = ""+diffMinutes;
}
if(diffSeconds < 10){
seconds = "0"+diffSeconds;
}else{
seconds = ""+diffSeconds;
}
txtTimer.setText(diffDays+"D "+hours+":"+minutes+":"+seconds);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Handler method for timer: 计时器的处理程序方法:
try {
final Handler timerHandler = new Handler();
updater = new Runnable() {
@Override
public void run() {
try {
checkingCondition();
} catch (Exception exp) {
exp.printStackTrace();
}
timerHandler.postDelayed(updater,500);
}
};
timerHandler.post(updater);
} catch (Exception exp) {
exp.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.