简体   繁体   English

为什么我的年度闹钟不能正常工作?

[英]Why does my yearly alarm not work properly?

i'm trying to set alarm for yearly events with this code我正在尝试使用此代码为年度事件设置警报

MyAlarmReceiver.java MyAlarmReceiver.java

mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
              
Intent intent1 = new Intent(context, AlarmReceiver.class);
intent1.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(mReceivedID));
mPendingIntent = PendingIntent.getBroadcast(context, mReceivedID, intent1, PendingIntent.FLAG_CANCEL_CURRENT);

Calendar calendar = Calendar.getInstance();
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

if(cal.isLeapYear(calendar.get(Calendar.YEAR))) {
    mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
         calendar.getTimeInMillis() + (86400000L*366), mPendingIntent);
} else {
    mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
         calendar.getTimeInMillis() + (86400000L*365), mPendingIntent);}
}

But it only works with 365 days even the year is definitely a leap year?但它只适用于 365 天,即使一年肯定是闰年?

The java.util.GregorianCalendar.isLeapYear() method determines if the given year passed as a parameter to the function is a leap year or not and returns true if the given year is a leap year and false otherwise. java.util.GregorianCalendar.isLeapYear()方法确定作为参数传递给函数的给定年份是否是闰年,如果给定年份是闰年则返回true否则返回false

Syntax:句法:

public boolean isLeapYear(int year)

Parameters: This function accepts a single integer parameter year that represents the year which the function needs to check for whether it is a leap year or not.参数:此函数接受单个整数参数 year 表示该函数需要检查是否为闰年的年份。

Return Values: The function returns a boolean value.返回值:该函数返回一个布尔值。 If the year passed as a parameter is a leap year, it returns true and false otherwise.如果作为参数传递的年份是闰年,则返回 true 和 false 否则。

Example:例子:

import java.io.*;
import java.util.*;
  
class GFG {
     public static void main(String[] args) {
      
      // Create a new calendar
      GregorianCalendar c = (GregorianCalendar) GregorianCalendar.getInstance();
  
      // Display the current date and time
      System.out.println("Current Date and Time : " + c.getTime());
  
      int year = c.get(GregorianCalendar.YEAR);
      if(c.isLeapYear(year)) {
           System.out.println(year + " is leap year");
      } else {
          System.out.println(year + " is Not a leap year");
      }
   }
}

Reference : isLeapYear参考isLeapYear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM