# Output the required palindrome date

·During the Spring Festival in 2020, a special date has attracted everyone's attention: February 2, 2020. Because if this date is written as an 8-digit number in the format of "yyyymmdd", it is 20200202, which is exactly a palindrome number. We call such a date palindromic date. Some people say that 20200202 is a special day of "once in a thousand years". Xiaoming doesn't agree with this, because the next palindrome date will be less than two years later: 20211202, that is, December 2, 2021. Others said that 20200202 is not only a palindrome date, but also an ABABBABA palindrome date. Xiaoming doesn't agree with this either, because the next ABABBABA palindrome date will come about in about 100 years: 21211212, that is, December 12, 2121. It is not "once in a thousand years", but "twice in a thousand years" at most.

·Given an 8-digit date, please calculate the next palindrome date and the next ABABBABA palindrome date after the date.

·Input Description: the input contains an eight digit integer N, indicating the date. For all evaluation cases, 10000101 ≤ N ≤ 89991231, ensure that N is 8 digits of a legal date

·Output Description: output two lines, each with one eight digit. The first line represents the next palindrome date, and the second line represents the next ABABBABA palindrome date.

## (1) Programming implementation

At p03 Create PalindromicDate class in T06 package

### 1. Judge whether the input date is legal

Create a static method isLegalDate(String strDate)

```  package p03.t06;

/**
* Function: output the required palindrome date
* Author: Li Yue
* Date: May 19, 2022
*/
public class PalindromicDate {
/**
* Judge whether the date is legal
*
* @param strDate
* @return true-Legal, false illegal
*/
private static boolean isLegalDate(String strDate) {
int year, month, day;
year = Integer.parseInt(strDate.substring(0, 4));
month = Integer.parseInt(strDate.substring(4, 6));
day = Integer.parseInt(strDate.substring(6));

// Use reverse thinking to deal with
if (year < 1000 || year > 8999) return false;
if (month < 1 || month > 12) return false;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day < 1 || day > 31) return false;
} else if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { // Leap year judgment
if (day < 1 || day > 29) return false;
} else {
if (day < 1 || day > 28) return false;
}
} else {
if (day < 1 || day > 30) return false;
}
return true;
}
}
```

### 2. Create a main method to test whether the input date is legal

·Judge whether the input date is legal and give corresponding prompt

```public static void main(String[] args) {
String strDate;
int year, month, day;
Scanner sc = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

System.out.print("Enter an 8-digit date:");
strDate = sc.next();

if (isLegalDate(strDate)) {
System.out.println("[" + strDate + "]Is a legal date~");
} else {
System.out.println("[" + strDate + "]Is an illegal date~");
}
}
```

### 3. Judge whether the date is the palindrome date

·Define the static method isPalindromicDate() method

```    /**
*
* @param strDate
* @retrun true-Palindrome, false non palindrome
*/
private static boolean isPalindromicDate(String strDate) {
for (int i = 0; i<4; i++) {
if (strDate.charAt(i) != strDate.charAt(7 - i)) return  false;
}
return true;
}
}
```

### 4. Modify the main method to output the first palindrome date after this date

·Complete the first task: output the first palindrome date after this date

```  //User entered date as starting point
year = Integer.parseInt(strDate.substring(0, 4));
month = Integer.parseInt(strDate.substring(4, 6));
day = Integer.parseInt(strDate.substring(6));
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month - 1);
calendar.set(Calendar.DAY_OF_MONTH,day);
//Enter the first palindrome date after this date
while (true) {
strDate = sdf.format(calendar.getTime());
if(isPalindromicDate(strDate)) {
break;
}
```

·View run results

### 5. Judge whether it is ABABBABA palindrome date

·Define static method isabababapalindromicdate()

```/**
* Judge whether it is ABABBABA palindrome date
*
* @param strDate
* @return true-ABABBABA Type palindrome date, false- non ABABBABA type palindrome date
*/
private static boolean isABABBABAPalindromicDate(String strDate) {
if (isPalindromicDate(strDate)) {
if (strDate.charAt(0) == strDate.charAt(2) && strDate.charAt(1) == strDate.charAt(3)) return true;
}
return false;
}
```

### 6. Modify the main method to output the first ABABBABA palindrome date after this date

·Complete the second task: output the first ABABBABA palindrome date after this date

```//Output the first ABABBABA palindrome date after this date
String strDate2 = strDate;
Calendar calendar2 = calendar;
while (true) {
calendar2.add(Calendar.DAY_OF_MONTH, 1); // 1 day later
strDate2 = sdf.format(calendar2.getTime());
if (isABABBABAPalindromicDate(strDate2)) {
break; //Find a palindrome date and jump out of the loop
}
}
System.out.println("First after this date ABABBABA Type palindrome date:" + strDate2);
```

### 8. Run the program to view the results

·Enter a valid date

```package p03t06;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
import java.util.regex.Pattern;

/**
* Function:
* Author: Li Yue
* Date: May 19, 2022
*/
public class PalindromicDate {
public static void main(String[] args) {
String strDate;
int year, month, day;
Scanner sc = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

System.out.print("Enter an 8-digit date:");
strDate = sc.next();

if (isLegalDate(strDate)) {
System.out.println("[" + strDate + "]Is a legal date~");
// Take the legal date entered by the user as the starting point of the date cycle
year = Integer.parseInt(strDate.substring(0, 4));
month = Integer.parseInt(strDate.substring(4, 6));
day = Integer.parseInt(strDate.substring(6));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
// Task 1. Output the first palindrome date after this date
String strDate1 = strDate;
Calendar calendar1 = calendar;
while (true) {
calendar1.add(Calendar.DAY_OF_MONTH, 1); // 1 day later
strDate1 = sdf.format(calendar1.getTime());
if (isPalindromicDate(strDate1)) {
break; // Find a palindrome date and jump out of the loop
}
}
System.out.println("The first palindrome date after this date:" + strDate1);
// Task 2. Output the first ABABBABA palindrome date after this date
String strDate2 = strDate;
Calendar calendar2 = calendar;
while (true) {
calendar2.add(Calendar.DAY_OF_MONTH, 1); // 1 day later
strDate2 = sdf.format(calendar2.getTime());
if (isABABBABAPalindromicDate(strDate2)) {
break; // Find a palindrome date and jump out of the loop
}
}
System.out.println("First after this date ABABBABA Type palindrome date:" + strDate2);
} else {
System.out.println("[" + strDate + "]Is an illegal date~");
}
}

/**
* Judge whether the date is legal
*
* @param strDate
* @return true-Legal, false illegal
*/
private static boolean isLegalDate(String strDate) {
int year, month, day;
year = Integer.parseInt(strDate.substring(0, 4));
month = Integer.parseInt(strDate.substring(4, 6));
day = Integer.parseInt(strDate.substring(6));

// Use reverse thinking to deal with
if (year < 1000 || year > 8999) return false;
if (month < 1 || month > 12) return false;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day < 1 || day > 31) return false;
} else if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { // Leap year judgment
if (day < 1 || day > 29) return false;
} else {
if (day < 1 || day > 28) return false;
}
} else {
if (day < 1 || day > 30) return false;
}
return true;
}

/**
* Judge whether it is palindrome date
*
* @param strDate
* @return true-Palindrome date, false non palindrome date
*/
private static boolean isPalindromicDate(String strDate) {
for (int i = 0; i < 4; i++) {
// Use reverse thinking
if (strDate.charAt(i) != strDate.charAt(7 - i)) return false;
}
return true;
}

/**
* Judge whether it is ABABBABA palindrome date
*
* @param strDate
* @return true-ABABBABA Type palindrome date, false- non ABABBABA type palindrome date
*/
private static boolean isABABBABAPalindromicDate(String strDate) {
if (isPalindromicDate(strDate)) {
if (strDate.charAt(0) == strDate.charAt(2) && strDate.charAt(1) == strDate.charAt(3)) return true;
}
return false;
}
}
```

### Case: calculate the number of days since the founding of the people's Republic of China

```package p03t06;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* Function:
* Author: Li Yue
* Date: May 19, 2022
*/
public class LifeiOfPRC {
public static void main(String[] args) {
// Create a simple date format object
SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day hh:mm:ss");
// Method 1: use Date class
Date foundDate = new Date(1949, 9, 1, 8, 0, 0 );
System.out.println("Date of establishment:" + sdf.format(foundDate));
Date currentDate = new Date();
System.out.println("Current date:" + sdf.format(currentDate));
long interval = 0;//Time interval (milliseconds)
interval = currentDate.getTime() - foundDate.getTime();
System.out.println("The people's Republic of China was founded" + interval + "millisecond");
System.out.println("The people's Republic of China was founded" + (interval / 1000) + "second");
System.out.println("The people's Republic of China was founded" + (interval / 1000 / 60)  + "branch");
System.out.println("The people's Republic of China was founded" + (interval /1000 / 60 / 60)+ "hour");
System.out.println("The people's Republic of China was founded" + (interval /1000 / 60 / 24) + "day");

// Method 2: use the Calendar class
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, 1949);
calendar1.set(Calendar.MONDAY, 10);
calendar1.set(Calendar.DAY_OF_MONTH, 1);
calendar1.set(Calendar.HOUR, 8);
calendar1.set(Calendar.MINUTE, 0);
calendar1.set(Calendar.SECOND, 0);
System.out.println("Date of establishment:" + sdf.format(calendar1.getTime()));
Calendar calendar2 =Calendar.getInstance();
System.out.println("Current date:" + sdf.format(calendar2.getTime()));
interval = calendar2.getTime().getTime() - calendar1.getTime().getTime();
System.out.println("The people's Republic of China was founded" + interval + "millisecond");
System.out.println("The people's Republic of China was founded" + (interval / 1000) + "second");
System.out.println("The people's Republic of China was founded" + (interval / 1000 / 60)  + "branch");
System.out.println("The people's Republic of China was founded" + (interval /1000 / 60 / 60)+ "hour");
System.out.println("The people's Republic of China was founded" + (interval / 1000 / 60 / 24) + "day");
}
}
```

·View run results

Tags: Java intellij-idea

Posted by anon on Fri, 03 Jun 2022 03:50:55 +0530