[英]Calculate time difference between current and future time
I want to calculate time difference in milliseconds from current time of a day(11 am , 1 october,2012) and time at midnight for the same day (11 pm 59 m 59s , 1 october , 2012. 我想计算当天时间(2012年10月1日上午11点)和同一天午夜时间(晚上11点59分59秒,2012年10月1日)的时间差(以毫秒为单位)。
I have tried this 我试过这个
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 59);
cal.add(Calendar.HOUR, 23);
cal.add(Calendar.MINUTE, 59);
cal.getTime().getTime() - today.getTime();
here today is the current date. 今天是今天的日期。
But when i print long values of cal and today , the time difference if of 86400 approx one day. 但是当我打印出长时间的cal值和今天的时间差时,如果是86400大约一天。
Use cal.set()
instead of cal.add()
使用cal.set()
而不是cal.add()
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
long diff = cal.getTime().getTime() - today.getTime();
You can set your date to newly created Calendar instance.. And then compare it with current instance using getTimeInMillis()
您可以将日期设置为新创建的Calendar实例..然后使用getTimeInMillis()
将其与当前实例进行比较
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.DATE, 1);
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.YEAR, 2012);
long difference = cal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.