Previous and Next Date Palindrome


In the given year, there will be only one palindromic date. Hence, to find previous palindromic date, you need to decrements the year and convert it to palindromic date.

Example: for 2030, valid month and day will be 03 and 02 which will lead to 2030/03/02.

So, it must be clear that palindromic date in this case is only possible when:

Year is of 4 digits (not for 10000) as months and number of days are in two digits
Last two digits of years might be {10, 20, 30, 40, 50, 60, 70, 80, 90, 01, 11, 21}
First two digits of years should be mirror image of 1-31 (with month and year validity).
Using rule 2 and 3, it can be checked in O(1) whether a given year has palindromic date or not in the given format. Let us assume that valid range of year is from 9999-0001, then it can be done in linear order to find previous palindromic date.
    package com.kartik.date.palindrome; import java.util.GregorianCalendar; public class DatePalindrome { public static void main(String[] args) { String date = "1001/10/01"; System.out.println("Enter date is --"+date); System.out.println("----->>>Given Date previous Palindrome<<<--- "); System.out.println("YYYY/DD/MM"); findPrevDate(date); System.out.println("----->>>Given Date Next Palindrome<<<--- "); System.out.println("YYYY/DD/MM"); findNextDate(date); } static void findPrevDate(String input) { int year = extractYear(input); // Extract first 4 digits --year; // limit is 101 because 0000 will not give you valid month and day. for (; year >= 101; --year) { if (hasPalindromic(year)) { writeDate(year); } } } static void findNextDate(String input) { int year = extractYear(input); --year; for (; year <= 9999; ++year) { if (hasPalindromic(year)) { writeDate(year); } } } static boolean hasPalindromic(int year) { int first2 = year % 100; int last2 = year / 100; boolean second_valid = false; boolean first_valid = false; switch (last2) { case 10: case 20: case 30: case 40: case 50: case 60: case 70: case 80: case 90: case 1: case 11: case 21: second_valid = true; break; } if (!second_valid) return false; switch (first2) { case 10: case 20: case 30: case 40: case 50: case 60: case 70: case 80: case 90: case 1: case 11: case 21: case 31: case 41: case 51: case 61: case 71: case 81: case 91: case 2: case 12: case 22: case 32: case 42: case 52: case 62: case 72: case 82: case 92: case 3: case 13: first_valid = true; break; } if (!first_valid) return false; // reverse digit such that 2 yields 20 int month = reverseDigitsOn100(last2); int day = reverseDigitsOn100(first2); GregorianCalendar cal = (GregorianCalendar) GregorianCalendar .getInstance(); if (month == 2) { if (day <= 28) return true; if (cal.isLeapYear(year) && day == 29) return true; if (day > 28) return false; } switch (month) { case 4: case 6: case 9: case 11: if (day > 30) return false; } // For remaining months, check is already made as first part of year is // gives day in range of [1..31]. return true; } static int reverseDigitsOn100(int n) { int r = 0; if (n == 0) return 0; r = r * 10; r = r + n % 10; reverseDigitsOn100(n / 10); return r; } static long reverse(int number) { int reversedNumber = 0; int temp = 0; while (number > 0) { temp = number % 10; reversedNumber = reversedNumber * 10 + temp; number = number / 10; } return reversedNumber; } static int extractYear(String input) { return Integer.parseInt(input.substring(0, 4)); } static void writeDate(int input) { long rev = reverse(input); String reverseYear = String.valueOf(rev); if (String.valueOf(input).length() < 4) { reverseYear = reverseYear + "0"; } if (reverseYear.length() < 4) { reverseYear = "0" + reverseYear; } String dd = reverseYear.substring(0, 2); String mm = reverseYear.substring(2, 4); StringBuffer br = new StringBuffer(); br.append(input); br.append('/'); br.append(dd); br.append('/'); br.append(mm); System.out.println(br.toString()); } }
    Enter date is --1001/10/01 ----->>>Given Date previous Palindrome<<<--- YYYY/DD/MM 192/29/10 191/19/10 190/09/10 182/28/10 181/18/10 180/08/10 172/27/10 171/17/10 170/07/10 162/26/10 161/16/10 160/06/10 152/25/10 151/15/10 150/05/10 142/24/10 141/14/10 140/04/10 132/23/10 131/13/10 130/03/10 122/22/10 121/12/10 120/02/10 113/31/10 112/21/10 111/11/10 110/01/10 103/30/10 102/20/10 101/10/10 ----->>>Given Date Next Palindrome<<<--- YYYY/DD/MM 1001/10/01 1002/20/01 1003/30/01 1010/01/01 1011/11/01 1012/21/01 1013/31/01 1020/02/01 1021/12/01 1022/22/01 1030/03/01 1031/13/01 1032/23/01 1040/04/01 1041/14/01 1042/24/01 1050/05/01 1051/15/01 1052/25/01 1060/06/01 1061/16/01 1062/26/01 1070/07/01 1071/17/01 1072/27/01 1080/08/01 1081/18/01 1082/28/01 1090/09/01 1091/19/01 1092/29/01 1101/10/11 1102/20/11 1103/30/11 1110/01/11 1111/11/11 1112/21/11 1113/31/11 1120/02/11 1121/12/11 1122/22/11 1130/03/11 1131/13/11 1132/23/11 1140/04/11 1141/14/11 1142/24/11 1150/05/11 1151/15/11 1152/25/11 1160/06/11 1161/16/11 1162/26/11 1170/07/11 1171/17/11 1172/27/11 1180/08/11 1181/18/11 1182/28/11 1190/09/11 1191/19/11 1192/29/11 2001/10/02 2002/20/02 2003/30/02 2010/01/02 2011/11/02 2012/21/02 2013/31/02 2020/02/02 2021/12/02 2022/22/02 2030/03/02 2031/13/02 2032/23/02 2040/04/02 2041/14/02 2042/24/02 2050/05/02 2051/15/02 2052/25/02 2060/06/02 2061/16/02 2062/26/02 2070/07/02 2071/17/02 2072/27/02 2080/08/02 2081/18/02 2082/28/02 2090/09/02 2091/19/02 2092/29/02 2101/10/12 2102/20/12 2103/30/12 2110/01/12 2111/11/12 2112/21/12 2113/31/12 2120/02/12 2121/12/12 2122/22/12 2130/03/12 2131/13/12 2132/23/12 2140/04/12 2141/14/12 2142/24/12 2150/05/12 2151/15/12 2152/25/12 2160/06/12 2161/16/12 2162/26/12 2170/07/12 2171/17/12 2172/27/12 2180/08/12 2181/18/12 2182/28/12 2190/09/12 2191/19/12 2192/29/12 3001/10/03 3002/20/03 3003/30/03 3010/01/03 3011/11/03 3012/21/03 3013/31/03 3020/02/03 3021/12/03 3022/22/03 3030/03/03 3031/13/03 3032/23/03 3040/04/03 3041/14/03 3042/24/03 3050/05/03 3051/15/03 3052/25/03 3060/06/03 3061/16/03 3062/26/03 3070/07/03 3071/17/03 3072/27/03 3080/08/03 3081/18/03 3082/28/03 3090/09/03 3091/19/03 3092/29/03 4001/10/04 4002/20/04 4003/30/04 4010/01/04 4011/11/04 4012/21/04 4013/31/04 4020/02/04 4021/12/04 4022/22/04 4030/03/04 4031/13/04 4032/23/04 4040/04/04 4041/14/04 4042/24/04 4050/05/04 4051/15/04 4052/25/04 4060/06/04 4061/16/04 4062/26/04 4070/07/04 4071/17/04 4072/27/04 4080/08/04 4081/18/04 4082/28/04 4090/09/04 4091/19/04 4092/29/04 5001/10/05 5002/20/05 5003/30/05 5010/01/05 5011/11/05 5012/21/05 5013/31/05 5020/02/05 5021/12/05 5022/22/05 5030/03/05 5031/13/05 5032/23/05 5040/04/05 5041/14/05 5042/24/05 5050/05/05 5051/15/05 5052/25/05 5060/06/05 5061/16/05 5062/26/05 5070/07/05 5071/17/05 5072/27/05 5080/08/05 5081/18/05 5082/28/05 5090/09/05 5091/19/05 5092/29/05 6001/10/06 6002/20/06 6003/30/06 6010/01/06 6011/11/06 6012/21/06 6013/31/06 6020/02/06 6021/12/06 6022/22/06 6030/03/06 6031/13/06 6032/23/06 6040/04/06 6041/14/06 6042/24/06 6050/05/06 6051/15/06 6052/25/06 6060/06/06 6061/16/06 6062/26/06 6070/07/06 6071/17/06 6072/27/06 6080/08/06 6081/18/06 6082/28/06 6090/09/06 6091/19/06 6092/29/06 7001/10/07 7002/20/07 7003/30/07 7010/01/07 7011/11/07 7012/21/07 7013/31/07 7020/02/07 7021/12/07 7022/22/07 7030/03/07 7031/13/07 7032/23/07 7040/04/07 7041/14/07 7042/24/07 7050/05/07 7051/15/07 7052/25/07 7060/06/07 7061/16/07 7062/26/07 7070/07/07 7071/17/07 7072/27/07 7080/08/07 7081/18/07 7082/28/07 7090/09/07 7091/19/07 7092/29/07 8001/10/08 8002/20/08 8003/30/08 8010/01/08 8011/11/08 8012/21/08 8013/31/08 8020/02/08 8021/12/08 8022/22/08 8030/03/08 8031/13/08 8032/23/08 8040/04/08 8041/14/08 8042/24/08 8050/05/08 8051/15/08 8052/25/08 8060/06/08 8061/16/08 8062/26/08 8070/07/08 8071/17/08 8072/27/08 8080/08/08 8081/18/08 8082/28/08 8090/09/08 8091/19/08 8092/29/08 9001/10/09 9002/20/09 9003/30/09 9010/01/09 9011/11/09 9012/21/09 9013/31/09 9020/02/09 9021/12/09 9022/22/09 9030/03/09 9031/13/09 9032/23/09 9040/04/09 9041/14/09 9042/24/09 9050/05/09 9051/15/09 9052/25/09 9060/06/09 9061/16/09 9062/26/09 9070/07/09 9071/17/09 9072/27/09 9080/08/09 9081/18/09 9082/28/09 9090/09/09 9091/19/09 9092/29/09

Previous
Next Post »