In: Computer Science
A date written in MMDDYYYY format is palindromic if it is read identically from left to right and from right to left. For example, the date February 2, 2020 (02022020). Develop a Java program (called Palindrome Dates.java) that identifies all palindromic dates in a given year. The program must have the following functionality: When running the program, the user is prompted to enter a year A. Afterwards, the program calculates all the palindromic dates and prints them on the screen (separated by a space) in MMDDYYYY format. Finally, the program prints on the screen the total number of palindromic dates in that year. Tip: use the LocalDate and DateTimeFormatter classes included in Java to handle dates. For this, it requires doing two imports: import java.time.LocalDate; import java.time.format.DateTimeFormatter; To iterate between dates you can use the following example which increments the start date by one day until it arrives just before the end date. for (LocalDate date = start; date.isBefore (end); date = date.plusDays (1)) { // Processing of the date contained in the date object }
Hi,
Please find below given code as per your requirement.
Let me know if you have any concern/doubt in this answer via comments.
Hope this answer helps you.
Thanks.
/********************JAVA CODE***********************/
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class PalindromeDates {
//Date format
static DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("MMddyyyy");
public static void main(String[] args) {
//Scanner class is used to take user input from console
Scanner sc = new Scanner(System.in);
//prompting user to enter year
System.out.print("Please enter year : ");
//storing year in variable
int year = Integer.valueOf(sc.nextLine());
//calling method which identifies palindroms in given year
identifyPalindromicDates(year);
}
/**
* This method takes year as parameter and prints all palindrom dates present in that year.
* @param year
*/
public static void identifyPalindromicDates(int year) {
//setting year in local date with first day of year
LocalDate start = LocalDate.of(year, 1,1);
//setting end date to next year to set boundary
LocalDate end = LocalDate.of(year+1, 1, 1);
int totalPanlindromeDates = 0;
for (LocalDate date = start; date.isBefore(end); date = date.plusDays (1)) {
//formatting date in string
String formattedDate = customFormatter.format(date);
//reversed the formatted string
String reverseDate = reverseString(formattedDate);
//if formatted and reversed string is same then it is palindrom
if(formattedDate.equals(reverseDate)) {
System.out.print(formattedDate + " ");
totalPanlindromeDates++;
}
}
System.out.println("\nTotal Palindrom in "+year+ " is : "+totalPanlindromeDates);
}
/**
* Reverse the given string in parameter and returns
* @param inputString
* @return
*/
public static String reverseString(String inputString) {
char[] strCharArray = new char[inputString.length()];
int counter = 0;
for (int i = inputString.length() - 1; i >= 0; i--) {
strCharArray[counter] = inputString.charAt(i);
counter++;
}
return String.valueOf(strCharArray);
}
}