0

One of the functionalities that my app provide is scheduling a specific event for the next day and I am getting the next day date using the following code:

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";

Problem is: if it is a common year February is 28 days and if it is a leap year February is 29 days, so if today is the 28th of February the next day will be the first of March in a common year and 29th of February in case of a leap year. How to handle this ? ... in other words how to detect leap and common years ?

Ahmed Samy
  • 1,006
  • 2
  • 11
  • 24

2 Answers2

1

You should be able to find a leap year just by casting to GregorianCalendar as follows:

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.");

}
solidak
  • 5,033
  • 3
  • 31
  • 33
-1

How to check selected year and month leap year or not please used this code

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;
}