Android 日历始终具有相同的日期
我希望当用户选中复选框时发出警报。这是我的代码:
if (cb1.isChecked())
{
Calendar calA = Calendar.getInstance();
//calA.set(Calendar.YEAR, Calendar.YEAR);
//calA.set(Calendar.MONTH, Calendar.MONTH);
//calA.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH);
calA.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
calA.set(Calendar.MINUTE, Calendar.MINUTE);
calA.set(Calendar.SECOND, Calendar.SECOND);
calA.set(Calendar.MILLISECOND, Calendar.MILLISECOND);
alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), pendingIntentA);
Toast.makeText(main.this, "Set time: " + String.valueOf(calA.getTime()), Toast.LENGTH_SHORT).show();
}
其他代码工作正常,如果我将小时和分钟设置为特定的,就像
calA.set(Calendar.HOUR_OF_DAY, 15);
calA.set(Calendar.MINUTE, 24);
它正在工作一样,但使用此代码我总是收到此 toast 消息: 3 月 5 日星期六 11:12:00 或 3 月 5 日星期六 11:12:13 (日期和时间都不好)
我的代码有什么问题?
i want an alarm to go off when user checks in a checkbox. Here is my code:
if (cb1.isChecked())
{
Calendar calA = Calendar.getInstance();
//calA.set(Calendar.YEAR, Calendar.YEAR);
//calA.set(Calendar.MONTH, Calendar.MONTH);
//calA.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH);
calA.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
calA.set(Calendar.MINUTE, Calendar.MINUTE);
calA.set(Calendar.SECOND, Calendar.SECOND);
calA.set(Calendar.MILLISECOND, Calendar.MILLISECOND);
alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), pendingIntentA);
Toast.makeText(main.this, "Set time: " + String.valueOf(calA.getTime()), Toast.LENGTH_SHORT).show();
}
Other codes are working fine, and if i set the hour and minute to specific ones, like
calA.set(Calendar.HOUR_OF_DAY, 15);
calA.set(Calendar.MINUTE, 24);
it's working, but with this code i always get this toast message:
Sat Mar 05 11:12:00 or Sat Mar 05 11:12:13
(neither the date nor the time is good)
What is wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Calendar.HOUR_OF_DAY
是一个常量,恰好是一个整数。当您
实际上将一天中的小时设置为为此常量选择的任何数字。该值相对于一天中的时间没有实际意义,因此它会产生垃圾结果。
当您获取日历时,它默认设置为当前时间,因此如果这就是您想要的,只需不要设置时间:
如果您随后想要设置“未来 5 分钟”之类的时间,请执行以下操作
:仍然得到不正确的时间,请验证系统时间设置是否正确。
来源:
日历
文档Calendar.HOUR_OF_DAY
is a constant, which just so happens to be an integer.When you
you are in fact setting the hour of day to whatever number happens to have been chosen for this constant. This value has no real meaning in relation to the hour of day, so it'll produce a garbage result.
When you get the calendar, it's by default set to the current time, so if that's what you're going for, simply don't set the time:
If you then want to set a time like "5 minutes into the future", do something like:
If you still get an incorrect time, verify that the system time is set correctly.
Source:
Calendar
documentationCalendar calA = Calendar.getInstance();
返回一个日历对象,其区域设置基于系统设置,并且其时间字段已使用当前日期和时间进行初始化。Sebastian P 是对的,
Calendar.HOUR_OF_DAY
是一个常量,是一个用于引用实际小时值的键/索引。请参阅 http://developer.android.com/reference/java/util/Calendar。 html
Calendar calA = Calendar.getInstance();
returns a calendar object whose locale is based on system settings and whose time fields have been initialized with the current date and time.Sebastian P is right that
Calendar.HOUR_OF_DAY
is a constant, a key/index used for referencing the actual hour value.See http://developer.android.com/reference/java/util/Calendar.html