[英]how to calculate the difference between 2 date in android?
i am creating a method that calculate the difference between 2 date (END DATE _ CURRENT DATE). 我正在创建一种计算两个日期(结束日期_当前日期)之间的差异的方法。 i want to display the date format in months , days , hours, minutes, seconds
我想以月,日,小时,分钟,秒为单位显示日期格式
like this format : 2 months.3 days. 像这样的格式:2个月。3天。 8 hours.
8小时。 12 min.
12分钟 32 second
32秒
can anyone help me ??? 谁能帮我 ??? i will appreciate that.
我会感激的。
public void getDiffDate() {
String dateStop = "12.6.2014";
long now = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
Date d1 = null;
Date d2 = null;
try {
d1 = new Date(now);
d2 = format.parse(dateStop);
} catch (ParseException e) {
e.printStackTrace();
}
long difference = d2.getTime() - d1.getTime();
long differenceBack = difference;
differenceBack = difference / 1000;
int secs = (int) (differenceBack % 60);
differenceBack /= 60;
int mins = (int) (differenceBack % 60);
differenceBack /= 60;
int hours = (int) (differenceBack % 24);
TextView txtDate = (TextView) findViewById(R.id.txtDifDate);
String resultDate = hours + "H" + " :" + mins + "M" + " :" + secs + "S";
txtDate.setText(resultDate);
}
actually i am getting the result but it is not correct so also i need to fix this error. 实际上我正在得到结果,但是它是不正确的,所以我还需要修复此错误。
Try this: 尝试这个:
String dateStart = "01/14/2012 09:29:58";
String dateStop = "01/15/2012 10:31:48";
//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.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.");
} catch (Exception e) {
e.printStackTrace();
}
//EDIT //编辑
Personally I would recommand you Joda Time. 我个人建议您乔达·时间(Joda Time)。 Example of Joda Time:
乔达时间的例子:
String dateStart = "01/14/2012 09:29:58";
String dateStop = "01/15/2012 10:31:48";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
DateTime dt1 = new DateTime(d1);
DateTime dt2 = new DateTime(d2);
System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");
System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");
} catch (Exception e) {
e.printStackTrace();
}
First, remove: long differenceBack = difference; 首先,删除:long differentBack =差异;
long differenceBack = difference;
differenceBack = difference / 1000;
Should be: 应该:
long differenceBack = difference / 1000;
First you need hours, to do this: 首先,您需要几个小时才能执行此操作:
int hours = differenceBack/(60*60);
Than you have the remaining seconds: 比剩下的秒数多:
int remaining = differenceBack%(60*60)
than again check how many minutes you have in the remaining seconds: 然后再检查剩余几秒钟内的分钟数:
int minutes = remaining/60;
check how many seconds you have left: 检查剩下的秒数:
int seconds = remaining%60;
You began right, by converting milliseconds to seconds. 您可以通过将毫秒转换为秒来开始正确的工作。 1 minute = 60 seconds, 1 hour = 60 minutes, or 60*60 seconds.
1分钟= 60秒,1小时= 60分钟,或60 * 60秒。 Good luck.
祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.