蓝桥杯,java回文日期

import java.text.ParseException;
import java.time.LocalDate;
import java.util.Scanner;
public class P2 {
    public static void main(String[] args) throws ParseException {
        LocalDate date = LocalDate.now();
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        int year = Integer.parseInt(s.substring(0, 4));
        int month = Integer.parseInt(s.substring(4, 6));
        int day = Integer.parseInt(s.substring(6, 8));
        date = date.withYear(year);
        date = date.withMonth(month);
        date = date.withDayOfMonth(day);
        date = date.plusDays(1);
        String c = null;
        String d = null;
        Boolean j = false;
        while (true){
            String s1 = date.toString().replace("-", "");
            if(a(s1)){
            if(!j){
                c = s1;
                 j=ture;//已经找到第一个了,把j赋值为ture,停止找下一个
            }
            if(b(s1)){
                d = s1;
                break;
            }
            }
            date = date.plusDays(1);
        }
        System.out.println(c);
        System.out.println(d);

    }
    static boolean a(String str){
        for (int i = 0; i < str.length()/2; i++) {
            if(str.charAt(i) != str.charAt(str.length()-1-i)){
                return false;
            }
        }
        return true;
    }
    static boolean b(String str){
        char[] chars = str.toCharArray();
        return chars[0] == chars[2] && chars[2] == chars[5] && chars[5] == chars[7] && chars[1] == chars[3] && chars[3] == chars[4] && chars[4] == chars[6];
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值