[英]Date difference timer of current and past date in android
我正在努力使當前日期和過去日期有所不同,而現在我已經很完美了。 但是我想要的是計時器,如果我的當前時間是1:02,過去的時間是12:02,那么計時器應該給我這樣的結果:01:02:02,01:00:03,01:00 :03,01:00:04。 我知道這不是那么復雜的問題,但是我無法找到解決方案。 現在我正在工作的代碼如下:
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();
}
提前致謝
我已經解決了我的問題,我在這里為其他人提供答案。 我制作了一種方法,可以與計時器一起計算以天,小時,分鍾和秒為單位的日期差。
日期差的計算方法:
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();
}
}
}
計時器的處理程序方法:
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.