在 Android 上计算日期
好吧,我目前正在为 Android 构建一个应用程序,我需要存储一天并计算距离那一天到来还有多少天。
我将这一天存储在共享首选项中。首先我初始化日历。
Calendar next = Calendar.getInstance();
Calendar now = Calendar.getInstance();
然后我设置“下一个”日历
nday = prefs.getInt("d", 0);
nmonth = prefs.getInt("m",0);
nyear = prefs.getInt("y",0);
next.set(nyear, nmonth, nday);
然后我这样做来计算还剩多少天。
diff =next.getTimeInMillis()-now.getTimeInMillis();
diffDays = diff / (24 * 60 * 60 * 1000);
output.setText(diffDays + " Days left");
问题就在这里。直到 2 天前,计算器都运行良好。当它应该说“3 天”时,它写的是“2 天”,但有一天仍然出错。如果我尝试关闭并打开应用程序,有时它会计算正确的日期,有时会错过一天......有人可以理解出了什么问题吗?我有 diff 和 diffDays 一样长。我尝试将它们转换为 int 但我仍然遇到同样的问题,有时它写剩余 3 天,有时写 2....
好吧,我找到了如何解决这个问题。似乎 getInstance 以毫秒为单位存在差异,所以我
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY,00);
now.set(Calendar.MINUTE ,00);
now.set(Calendar.SECOND,00);
now.set(Calendar.MILLISECOND,00);
从共享首选项中获取了这一天
nday = extras.getInt("nDay");
nmonth = extras.getInt("nMonth");
nyear = extras.getInt("nYear");
//set the calendar
next.set(nyear, nmonth, nday, 00, 00,00);
next.set(Calendar.MILLISECOND,00);
,最后计算出差异,
long diff = 0;
diff = next.getTimeInMillis()-now.getTimeInMillis();
diffDays = diff / (24 * 60 * 60 * 1000);
output.setText(diffDays + " Days");
现在我得到了真正的差异,没有任何错误,感谢大家的帮助!
Well i'm currently building an app for android and i need to store a day and count how many days until that day comes.
I store the day on shared prefs. First i initialize the calendars.
Calendar next = Calendar.getInstance();
Calendar now = Calendar.getInstance();
Then i set the "next" calendar
nday = prefs.getInt("d", 0);
nmonth = prefs.getInt("m",0);
nyear = prefs.getInt("y",0);
next.set(nyear, nmonth, nday);
Then i do this to calculate how many days left.
diff =next.getTimeInMillis()-now.getTimeInMillis();
diffDays = diff / (24 * 60 * 60 * 1000);
output.setText(diffDays + " Days left");
And here is the problem. The calculator was working great until 2 days ago. When it supposed to say "3 days" it was writing "2 days" and it still goes one day wrong. If i try close and open the app, sometimes it calculates the days correct and sometimes it misses one day... Can someone understands whats wrong? I have diff and diffDays as long. I tried cast them as int but i still got the same problem, sometimes it writes 3 days left, sometimes 2....
ok i found out how to solve this. It seems that the getInstance have difference in milliseconds so i did this
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY,00);
now.set(Calendar.MINUTE ,00);
now.set(Calendar.SECOND,00);
now.set(Calendar.MILLISECOND,00);
get the day from shared prefs
nday = extras.getInt("nDay");
nmonth = extras.getInt("nMonth");
nyear = extras.getInt("nYear");
//set the calendar
next.set(nyear, nmonth, nday, 00, 00,00);
next.set(Calendar.MILLISECOND,00);
and finally calculate the difference
long diff = 0;
diff = next.getTimeInMillis()-now.getTimeInMillis();
diffDays = diff / (24 * 60 * 60 * 1000);
output.setText(diffDays + " Days");
now i get the real difference without any mistakes, thanks everyone for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为它隐式转换为 long,所以您会损失一些分钟和小时。
将
diffDays
声明为 double,然后您可以向用户显示何时有2.5 天
,并且不会将其显示为2 天
>。或者,您可以取天数的整数部分,并根据小数计算小时:这样您就可以得到分钟。只需确保您确实将
diffDays
声明为 double 或 float 即可。Because it is implicitly casting to long, so you are losing some of your minutes and hours.
Declare
diffDays
as double, then you can show to the user when there are, for example2.5 days
and it won't show it as2 days
. Or, you could take the integer part for the days, and calculate the hour from the fraction:This gives you the minute. Just make sure you did declare
diffDays
as double or float.还可以使用日历来计算日期。
Also use the calendar for calculating dates.