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. 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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.