我有两个日期,一个是未来日期,另一个是当前日期。 我希望得到这两个日期之间的区别我正在使用下面的代码,但输出不是我想通过这个帮助我。我是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秒我希望它显示像倒计时,请帮助我。
答案 0 :(得分:3)
你总是用整个时差计算。
首先必须计算天数,然后计算剩余时间。然后用剩余的分钟等等。
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();
}
}
答案 1 :(得分:0)
下面的代码可能有效,只是试一试...请注意,这不是我觉得有效的方法......
long days = diff / (24*60*60*1000);
long hours=(diff/(60*60*1000))-(days*24);
long minutes=(diff/(60*1000))-(days*24*60)-(hours*60);
long seconds=(diff/(1000))-(days*24*60*60)-(hours*60*60)-(minutes*60);
答案 2 :(得分:0)
我对davidtrautmann的答案有一些修改,而不是davidtrautmann。用户可以找到更好的方法来获取当前时间和过去日期之间的差异,包括天,小时,小时和秒。用户还可以根据自己的要求进行修改。
public String getDifferentBetweenTwoDates(String pastDate ){//your input date format should be same as your dateformater
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date inputDate = null;
try {
inputDate = format.parse(pastDate); // format.parse("2016-04-04 20:10:10");
}catch (ParseException e) {
e.printStackTrace();
}
Calendar pastCalendar = Calendar.getInstance();
if(inputDate!=null){
pastCalendar.setTime(inputDate);
}
long pastDateTimeInMilli = pastCalendar.getTimeInMillis();
Calendar currentCalendar = Calendar.getInstance();
long currentDateTimeInMilli = currentCalendar.getTimeInMillis();
String strDifference="";
long different = currentDateTimeInMilli - pastDateTimeInMilli;
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
int elapsedDays = (int)(different / daysInMilli);
different = different % daysInMilli;
if(elapsedDays>0){
strDifference = ""+elapsedDays;
return strDifference+" days ago";
}
int elapsedHours = (int)(different / hoursInMilli);
different = different % hoursInMilli;
if(elapsedHours>0){
strDifference = ""+elapsedHours;
return strDifference+" hours ago";
}
int elapsedMinutes = (int)(different / minutesInMilli);
different = different % minutesInMilli;
if(elapsedMinutes>0){
strDifference = ""+elapsedMinutes;
return strDifference+" minuts ago";
}
int elapsedSeconds = (int)(different / secondsInMilli);
if(elapsedSeconds>0){
strDifference = ""+elapsedSeconds;
return strDifference +" seconds ago";
}else{
return "just now";
}
}