我在应用程序中使用日历作为闹钟,但如果我使用日历,则 timeInMillis 是错误的。
我给你看一个例子:
// Get Current Time
Calendar calNow = Calendar.getInstance();
calNow.setTimeInMillis(System.currentTimeMillis());
calNow.add(Calendar.HOUR, calNow.get(Calendar.HOUR_OF_DAY));
calNow.add(Calendar.MINUTE, calNow.get(Calendar.MINUTE));
long now = calNow.getTimeInMillis();
long now
返回例如:1345848049917
now
必须与 System.currentTimeMillis()
相同,但事实并非如此,system
返回 1345846849921
现在
在我的时区:25.08.2012 - 00:40:49
System.currentTimeMillis()
是:25.08.2012 - 00:20:49
正好 20 分钟后,但为什么呢?
最佳答案
我不太确定你想做什么,但是这些行
calNow.add(Calendar.HOUR, calNow.get(Calendar.HOUR_OF_DAY));
calNow.add(Calendar.MINUTE, calNow.get(Calendar.MINUTE));
将一天中的小时和分钟加倍,将 00:20:49
变为 00:40:49
。
关于java - 日历返回错误时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12117459/