【蓝桥】 回文日期

记录一下被蓝桥折磨的时光之[回文日期]

import java.util.*; 

public class _回文日期 {
//	public static 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int year = Integer.valueOf(s.substring(0, 4));
		int month = Integer.valueOf(s.substring(4, 6)) - 1;
		int day = Integer.valueOf(s.substring(6, 8));
		
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		c.set(Calendar.DAY_OF_MONTH, day);
		c.add(Calendar.DAY_OF_MONTH, 1);
		
		boolean f1 = false, f2 = false;
		while(f1 != true || f2 != true) { // 考察逻辑条件(被折磨了头天*_*|||)
			String cur = toString(c);
			if (!f1 && isPalindrome(cur)) { // 考察逻辑条件(被折磨了头天*_*|||)
				System.out.println(cur);
				f1 = true;
			}
			if (f1 && isSpecialPalindrome(cur)) { // 考察逻辑条件(被折磨了头天*_*|||)
				System.out.println(cur);
				f2 = true;
			}
			c.add(Calendar.DAY_OF_MONTH, 1);
		}
	}
	
	public static boolean isPalindrome(String s) { // 判断是否为回文年份ABCDDCBA
		boolean f = true;
		char[] chs = s.toCharArray();
		int left = 0, right = chs.length - 1;
		while(left<right) {
			if (chs[left] != chs[right]) {
				f = false;
				break;
			}
			left++;
			right--;
		}
		return f;
	}
	
	public static boolean isSpecialPalindrome(String s) { // 判断是否为特殊回文年份ABABBABA
		if (!isPalindrome(s))return false;
		return s.charAt(0)==s.charAt(2) && s.charAt(1) == s.charAt(3);
	}
	
	public static String toString(Calendar c) {
		StringBuffer s = new StringBuffer();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		if (year<10) {
			s.append("000" + year);
		} else if (year<100) {
			s.append("00" + year);
		} else if (year < 1000) {
			s.append("0" + year);
		} else {
			s.append(year);
		}
		
		if (month<10) {
			s.append("0" + month);
		} else {
			s.append(month);
		}
		
		if (day < 10) {
			s.append("0" + day);
		} else {
			s.append(day);
		}
		return s.toString();
	}
}

欢迎各位大佬一起探讨,提提宝贵意见

你可能感兴趣的:(蓝桥杯,算法,回文日期,日期处理,Java)