Android 日历始终具有相同的日期

发布于 2024-10-15 08:33:02 字数 956 浏览 1 评论 0原文

我希望当用户选中复选框时发出警报。这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-10-22 08:33:02

Calendar.HOUR_OF_DAY 是一个常量,恰好是一个整数。

当您

calA.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);

实际上将一天中的小时设置为为此常量选择的任何数字。该值相对于一天中的时间没有实际意义,因此它会产生垃圾结果。

当您获取日历时,它默认设置为当前时间,因此如果这就是您想要的,只需不要设置时间:

与其他区域设置敏感的类一样,Calendar 提供了一个类方法 getInstance,用于获取这种类型的通常有用的对象。 Calendar 的 getInstance 方法返回一个 Calendar 对象,其日历字段已使用当前日期和时间进行初始化

日历 rightNow = Calendar.getInstance();

如果您随后想要设置“未来 5 分钟”之类的时间,请执行以下操作

calA.add(Calendar.MINUTE, 5);

:仍然得到不正确的时间,请验证系统时间设置是否正确。

来源:

Calendar.HOUR_OF_DAY is a constant, which just so happens to be an integer.

When you

calA.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);

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:

Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time:

Calendar rightNow = Calendar.getInstance();

If you then want to set a time like "5 minutes into the future", do something like:

calA.add(Calendar.MINUTE, 5);

If you still get an incorrect time, verify that the system time is set correctly.

Source:

烟雨凡馨 2024-10-22 08:33:02

Calendar 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文