Search code examples
javadatepalindrome

Date Palindrome finder is not functioning correctly (Java)


I'm quite new to programming, and I was trying to find the next nearest palindromic date that will occur and I wrote the following code. But the problem that I'm facing is that it keeps on printing out the same exact thing again and again rather than increasing the date or the month. I've tried to debug this but I'm not seeing where I'm going wrong, can anyone please let me know how to do this!

 public static void main(String[] args) {

    Date date = new Date();
    int year = 1900+date.getYear();
    int month = 1+date.getMonth();
    int currDate = date.getDate();

    String currDate1 = formatter(currDate);
    String month1 = formatter(month);

    String theDate = currDate1+month1+year;

    while(palindromeChecker(theDate)){

        if(month==12) {
            year++;
            month=1;
        }

        if(year%4 == 0 && year%100 !=0 && year%400 == 0) {
            if (month == 2 && currDate == 29) {
                month++;
                currDate = 1;
            }
        }
        else {
            if (month == 2 && currDate == 28) {
                month++;
                currDate = 1;
            }
        }
        switch (month){
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                if(currDate==31) month++; currDate=1; break;
            case 4: case 6: case 9: case 11:
                if(currDate==30) month++; currDate=1; break;
        }

        currDate++;

        theDate = formatter(currDate)+formatter(month)+year;

        System.out.println(theDate);
    }

}

public static String formatter(int a){
    return new DecimalFormat("00").format(a);
}

public static boolean palindromeChecker(String a){
    String b = new StringBuilder(a).reverse().toString();
    if(a.equals(b)) return false;
    else return true;
}

Solution

  • The exercise is a good one, but sorry to say, you are doing it the wrong way. I figure that about the worst thing I could do would be solving the problem for you and posting complete and/or executable code. So I will try to guide you to the right way of solving the task. Please feel free to follow up in comments if you try and encounter problems.

    java.time

    First, use LocalDate for a date, not Date. Despite the name the Date class does not represent a date but a point in time (and at that point in time it is never the same date everywhere on Earth). In contrast a LocalDate is exactly a date without time of day and without time zone. Just what you need. Also the Date class was always poorly designed and is now long outdated.

    For getting today’s date in your time zone use for example

        LocalDate date = LocalDate.now(ZoneId.systemDefault());
    

    You can pass a different time zone to the now method if you want.

    For converting the date into a string so that you can check whether it’s a palindrome use a DateTimeFormatter and the format method of LocalDate. You can use DateTimeFormatter.ofPattern() to construct the formatter. When all of this sounds vague or even incomprehensible, use your search engine to find examples and tutorials that will flesh it out a lot more, or check the documentation, use the links at the bottom.

    Your way of checking whether a string is a palindrome is very elegant, so you should stay with it.

    When one date turns out not to be a palindrome, use the plusDays method of LocalDate to add 1 day so you get the next day. LocalDate knows the next day, so you will not need to care about the number of days in each month nor about leap years, all of this is taken care of for you. Only remember that plusDays() returns a new LocalDate instance that you need to store into your variable.

    Links