android 如何处理闰年和平年

5ssjco0h  于 2023-02-17  发布在  Android
关注(0)|答案(2)|浏览(142)

我的应用程序提供的功能之一是为第二天安排一个特定的事件,我使用以下代码获得第二天的日期:

TimeZone timeZone = TimeZone.getTimeZone("GMT+05:30");
Calendar date = Calendar.getInstance();
date.setTimeZone(timeZone);
String refreshDate = (date.get(Calendar.DAY_OF_MONTH) + 1) + "/" + 
                     (date.get(Calendar.MONTH) + 1) + "/" + 
                     date.get(Calendar.YEAR) + "," + 
                     "12:00:0";

问题是:如果是平年2月是28天,如果是闰年2月是29天,那么如果今天是2月28日,那么第二天将是平年3月1日,如果是闰年,则是2月29日。如何处理这个问题?...换句话说,如何检测闰年和平年?

7uzetpgm

7uzetpgm1#

您应该能够通过转换为GregorianCalendar找到闰年,如下所示:

GregorianCalendar cal = (GregorianCalendar) cal;

if(cal.isLeapYear(year))
{
    System.out.print("Given year is leap year.");
}
else
{ 
    System.out.print("Given year is not leap year.");

}
n3h0vuf2

n3h0vuf22#

如何检查所选年份和月份是否为闰年*请使用此代码 *

public boolean isValidMonthOfDays( String month) {
    int bDay = Integer.parseInt(Prefs.getString(AUtils.PREFS.SUR_BIRTH_DAY,""));
    int bMonth = Integer.parseInt(Prefs.getString(AUtils.PREFS.SUR_BIRTH_MONTH,""));
    int bYear = Integer.parseInt(Prefs.getString(AUtils.PREFS.SUR_BIRTH_YEAR,""));
    String  monthValue ;
    int number_Of_DaysInMonth = 0;

    switch (month) {
        case "01":
            monthValue = "01";
            number_Of_DaysInMonth = 31;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of January");
            }else {
                AUtils.warning(context, "Invalid date of January");
                return false;
            }
            break;
        case "02":
            monthValue = "02";
            if (((bYear % 4 == 0) && (bYear % 100 != 0)) || (bYear % 400 == 0)) {
                number_Of_DaysInMonth = 29;
            } else {
                number_Of_DaysInMonth = 28;
            }
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of February");
            }else {
                AUtils.warning(context, "Invalid date of February");
                return false;
            }
            break;
        case "03":
            monthValue = "03";
            number_Of_DaysInMonth = 31;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of March");
            }else {
                AUtils.warning(context, "Invalid date of March");
                return false;
            }
            break;
        case "04":
            monthValue = "04";
            number_Of_DaysInMonth = 30;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of April");
            }else {
                AUtils.warning(context, "Invalid date of April");
                return false;
            }
            break;
        case "05":
            monthValue = "05";
            number_Of_DaysInMonth = 31;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of May");
            }else {
                AUtils.warning(context, "Invalid date of May");
                return false;
            }
            break;
        case "06":
            monthValue = "06";
            number_Of_DaysInMonth = 30;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of June");
            }else {
                AUtils.warning(context, "Invalid date of June");
                return false;
            }
            break;
        case "07":
            monthValue = "07";
            number_Of_DaysInMonth = 31;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of July");
            }else {
                AUtils.warning(context, "Invalid date of July");
                return false;
            }
            break;
        case "08":
            monthValue = "08";
            number_Of_DaysInMonth = 31;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of August");
            }else {
                AUtils.warning(context, "Invalid date of August");
                return false;
            }
            break;
        case "09":
            monthValue = "09";
            number_Of_DaysInMonth = 30;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of September");
            }else {
                AUtils.warning(context, "Invalid date of September");
                return false;
            }
            break;
        case "10":
            monthValue = "10";
            number_Of_DaysInMonth = 31;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of October");
            }else {
                AUtils.warning(context, "Invalid date of October");
                return false;
            }
            break;
        case "11":
            monthValue = "11";
            number_Of_DaysInMonth = 30;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of November");
            }else {
                AUtils.warning(context, "Invalid date of November");
                return false;
            }
            break;
        case "12":
            monthValue = "12";
            number_Of_DaysInMonth = 31;
            if (number_Of_DaysInMonth >= bDay && month.equals(monthValue)){
                AUtils.info(context, "Valid date of December");
            }else {
                AUtils.warning(context, "Invalid date of December");
                return false;
            }
            break;
    }

    return true;
}

相关问题