To find all previous and next palindrome dates in Java, we
can create a program that continuously finds and stores these dates until a
certain condition is met (like reaching a predefined limit or a specific date
range). Below is a complete implementation that finds all previous and next
palindrome dates within a given range:
Java Code Implementation
java
import
java.text.ParseException; import
java.text.SimpleDateFormat; import
java.util.ArrayList; import
java.util.Calendar; import
java.util.Date; import
java.util.List; public class
DatePalindrome { // Function to check if a given date is a
palindrome in "YYYYMMDD" format public static boolean isPalindrome(String
date) { int len = date.length(); for (int i = 0; i < len / 2; i++)
{ if (date.charAt(i) !=
date.charAt(len - i - 1)) { return false; } } return true; } // Function to format date as
"YYYYMMDD" public static String formatDate(Date
date) { SimpleDateFormat sdf = new
SimpleDateFormat("yyyyMMdd"); return sdf.format(date); } // Function to get the next date public static Date getNextDate(Date date)
{ Calendar calendar =
Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, 1); return calendar.getTime(); } // Function to get the previous date public static Date getPreviousDate(Date
date) { Calendar calendar =
Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, -1); return calendar.getTime(); } // Function to find all next palindrome
dates within a given range public static List<String>
getAllNextPalindromeDates(Date startDate, int limit) { List<String> palindromeDates =
new ArrayList<>(); Date nextDate =
getNextDate(startDate); while (palindromeDates.size() <
limit) { if
(isPalindrome(formatDate(nextDate))) {
palindromeDates.add(formatDate(nextDate)); } nextDate = getNextDate(nextDate); } return palindromeDates; } // Function to find all previous
palindrome dates within a given range public static List<String>
getAllPreviousPalindromeDates(Date startDate, int limit) { List<String> palindromeDates =
new ArrayList<>(); Date prevDate =
getPreviousDate(startDate); while (palindromeDates.size() <
limit) { if
(isPalindrome(formatDate(prevDate))) {
palindromeDates.add(formatDate(prevDate)); } prevDate =
getPreviousDate(prevDate); } return palindromeDates; } public static void main(String[] args)
throws ParseException { SimpleDateFormat sdf = new
SimpleDateFormat("yyyyMMdd"); Date date =
sdf.parse("20240804"); int limit = 10; // Limit to 10 previous and next palindrome
dates System.out.println("Current
Date: " + formatDate(date)); List<String> nextPalindromes =
getAllNextPalindromeDates(date, limit); System.out.println("Next
Palindrome Dates:"); for (String nextDate :
nextPalindromes) { System.out.println(nextDate); } List<String>
previousPalindromes = getAllPreviousPalindromeDates(date, limit); System.out.println("Previous
Palindrome Dates:"); for (String prevDate :
previousPalindromes) { System.out.println(prevDate); } } } |
Explanation
- isPalindrome(String
date): Checks if a given date string in "YYYYMMDD" format is
a palindrome.
- formatDate(Date
date): Converts a Date object to a string in "YYYYMMDD"
format.
- getNextDate(Date
date): Returns the next date.
- getPreviousDate(Date
date): Returns the previous date.
- getAllNextPalindromeDates(Date
startDate, int limit): Finds all next palindrome dates from the start
date until the specified limit is reached.
- getAllPreviousPalindromeDates(Date
startDate, int limit): Finds all previous palindrome dates from the
start date until the specified limit is reached.
- main:
Entry point of the program. It parses a given date, sets the limit for how
many palindrome dates to find, and prints the next and previous palindrome
dates.
Usage
- Current
Date: "20240804" is used as the example date.
- Limit:
Adjust the limit variable to control how many previous and next palindrome
dates you want to find.
Conclusion
This program effectively finds all previous and next
palindrome dates within a given range from a specified start date in Java.
Adjust the initial date and limit in the main method to test with different
dates and ranges.
0 Comments