本节任务:输入指定要求的回文日期
- 2020年春节期间,有一个特殊的日期引起了大家的注意:
2020年2月2日
。因为如果将这个日期按“yyyymmdd
”的格式写成一个8
位数是20200202
,恰好是一个回文数。我们称这样的日期是回文日期(palindromic date)
。有人表示20200202
是“千年一遇”的特殊日子。对此小明很不认同,因为不到2年之后就是下一个回文日期:20211202
即2021年12月2日。也有人表示20200202
并不仅仅是一个回文日期,还是一个ABABBABA
型的日期。对此小明也不认同,因为大约100年后就能遇到下一个ABABBABA
型的回文日期:21211212
即2121年12月12日。算不上“千年一遇”,顶多算“千年两遇”。 - 所以我们给定一个8位数的日期,需要我们计算一下该日期之后的下一个回文日期和下一个ABABBABA型回文日期各是哪一天。
(一)编程实现
1、首先我们需要判断输入的日期是否是合法的
- 创建一个静态的方法
isLegaDate(String strDate
package net.jia.p03.t06;
public class PalindromicDate {
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));
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) {
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、创建一个主方法,测试输入的日期是否是合法
- 需要判断输入的日期是不是合法的,并给出相应的提示
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("输入8位数构成的日期:");
strDate = sc.next();
if (isLegalDate(strDate)) {
System.out.println("[" + strDate + "]是合法日期~");
} else {
System.out.println("[" + strDate + "]是非法日期~");
}
}
- 运行程序,查看结果
3、判断输入的日期是不是回文日期
- 定义一个静态的方法
isPalindromicDate()
方法
private static boolean isPalindromiDate(String strDate) {
for (int i = 0; i < 4; i++) {
if (strDate.charAt(i) != strDate.charAt(7 - i)) return false;
}
return true;
}
4、修改主方法,输出该日期之后的第一个回文日期
- 输出该日期之后的第一个回文日期
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);
String strDate1 = "";
Calendar calendar1 = calendar;
while (true) {
calendar1.add(Calendar.DAY_OF_MONTH, 1);
strDate1 = sdf.format(calendar.getTime());
if (isPalindromiDate(strDate1)) {
break;
}
}
System.out.println("此日期之后的第一个回文日期:" + strDate1);
- 运行程序,查看结果
5、判断是不是ABABBABA型回文日期
- 定义一个静态方法
isABABBABAPalindromicDate()
private static boolean isABABBABADate(String strDate) {
if (isPalindromiDate(strDate)) {
if (strDate.charAt(0) == strDate.charAt(2) && strDate.charAt(1) == strDate.charAt(3)) return true;
}
return false;
6、修改主方法,输出该日期之后的第一个ABABBABA型回文日期
- 输出该日期之后的第一个ABABBABA型回文日期
String strDate2 = "";
Calendar calendar2 = calendar;
while (true) {
calendar2.add(Calendar.DAY_OF_MONTH, 1);
strDate2 = sdf.format(calendar2.getTime());
if (isABABBABADate(strDate2)) {
break;
}
}
System.out.println("此日期之后的第一个ABABBABA回文日期:" + strDate2);
7、完整的源代码如下
package net.jia.p03.t06;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class PalindromicDate {
public static void main(String[] args) {
String strDate = "";
int year, month, day;
Scanner sc = new Scanner(System.in);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
System.out.print("输入8位数构成的日期:");
strDate = sc.next();
if (isLegalDate(strDate)) {
System.out.println("[" + strDate + "]是合法日期~");
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);
String strDate1 = "";
Calendar calendar1 = calendar;
while (true) {
calendar1.add(Calendar.DAY_OF_MONTH, 1);
strDate1 = sdf.format(calendar.getTime());
if (isPalindromiDate(strDate1)) {
break;
}
}
System.out.println("此日期之后的第一个回文日期:" + strDate1);
String strDate2 = "";
Calendar calendar2 = calendar;
while (true) {
calendar2.add(Calendar.DAY_OF_MONTH, 1);
strDate2 = sdf.format(calendar2.getTime());
if (isABABBABADate(strDate2)) {
break;
}
}
System.out.println("此日期之后的第一个ABABBABA回文日期:" + strDate2);
}else {
System.out.println("[" + strDate + "]是非法日期~");
}
}
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));
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) {
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;
}
private static boolean isPalindromiDate(String strDate) {
for (int i = 0; i < 4; i++) {
if (strDate.charAt(i) != strDate.charAt(7 - i)) return false;
}
return true;
}
private static boolean isABABBABADate(String strDate) {
if (isPalindromiDate(strDate)) {
if (strDate.charAt(0) == strDate.charAt(2) && strDate.charAt(1) == strDate.charAt(3)) return true;
}
return false;
}
}
- 运行程序,查看结果
- 输入一个合法的日期
- 输入一个非法的日期