栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

输出指定要求的回文日期

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

输出指定要求的回文日期

  • 2020 年春节期间,有一个特殊的日期引起了大家的注意:2020 年 2 月 2 日。因为如果将这个日期按“yyyymmdd”的格式写成一个8 位数是20200202,恰好是一个回文数。我们称这样的日期是回文日期(palindromic date)。有人表示20200202 是“千年一遇”的特殊日子。对此小明很不认同,因为不到 2 年之后就是下一个回文日期:20211202 即 2021年 12月2日。也有人表示 20200202 并不仅仅是一个回文日期,还是一个 ABABBABA 型的回文日期。对此小明也不认同,因为大约 100 年后就能遇到下一个 ABABBABA 型的回文日期:21211212即2121 年 12 月 12 日。算不上“千年一遇”,顶多算“千年两遇”。

  • 输出指定要求的回文日期

1.判断日期是否合法


运行结果

代码:

package test;

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;
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Scanner sc = new Scanner(System.in);

        System.out.print("输入八位数构成的日期:");
        strDate = sc.next();

        // 判断日期是否合法
        if (isLegalDate(strDate)) {
            System.out.println(strDate + "是合法日期~");
        } 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;
    }
}

2.判断日期是否是回文日期

    
    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;
    }

3.修改主方法,输出该日期之后的第一个回文日期

// 判断日期是否合法
        if (isLegalDate(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); // 后推1天
                strDate1 = sdf.format(calendar1.getTime());
                if (isPalindromicDate(strDate1)) {
                    break;
                }
            }
            System.out.println("此日期之后的第一个回文日期:" + strDate1);

            String strDate2 = "";
            Calendar calendar2 = calendar;
            while (true) {
                calendar2.add(Calendar.DAY_OF_MONTH, 1); // 后推1天
                strDate2 = sdf.format(calendar2.getTime());
                if (isABABBABADate(strDate2)) {
                    break;
                }
            }
            System.out.println("此日期之后的第一个ABABBABA型回文日期:" + strDate2);
        } else {
            System.out.println(strDate + "是非法日期~");
        }
    }

4.判断是否是ABABBABA型回文日期

    
    private static boolean isABABBABADate(String strDate) {
        if (isPalindromicDate(strDate)) {
            if (strDate.charAt(0) == strDate.charAt(2) && strDate.charAt(1) == strDate.charAt(3)) {
                return true;
            }
        }
        return false;
    }
}

案例:计算中华人民共和国成立多少天


运行结果

代码

package test;

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

public class LIfeOfPRC {
    public static void main(String[] args) {
        //创建简单日期格式对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");

        System.out.println("方法一、采用Date类");
        Date foundDate = new Date(1949 -1900, 9, 1, 8, 0, 0);//1949-10-01
        System.out.println("成立日期:" +  sdf.format(foundDate));
        Date currentDate = new Date();
        System.out.println("当前日期:" + sdf.format(currentDate));
        long interval = 0; //时间间隔(毫秒数)
        interval = currentDate.getTime() - foundDate.getTime();
        System.out.println("中华人民共和国成立了" + interval + "毫秒");
        System.out.println("中华人民共和国成立了" + (interval /1000 + "秒"));
        System.out.println("中华人民共和国成立了" + (interval /1000 /60 + "分"));
        System.out.println("中华人民共和国成立了" + (interval /1000 / 60 /60 +"小时"));
        System.out.println("中华人民共和国成立了" + (interval /1000 /60 / 60 /24 + "天"));

        System.out.println();

        System.out.println("方法二、采用Calendar类");
        Calendar calendar1 = Calendar.getInstance(); //单例模式
        calendar1.set(Calendar.YEAR, 1949);
        calendar1.set(Calendar.MONTH, 9);
        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("成立日期:" + sdf.format(calendar1.getTime()));
        Calendar calendar2 = Calendar.getInstance();
        System.out.println("当前日期:" + sdf.format(calendar2.getTime()));
        interval = calendar2.getTime().getTime() - calendar1.getTime().getTime();
        System.out.println("中华人民共和国成立了" + interval + "毫秒" );
        System.out.println("中华人民共和国成立了" + (interval /1000 + "秒" ));
        System.out.println("中华人民共和国成立了" + (interval /1000 /60) + "分" );
        System.out.println("中华人民共和国成立了" + (interval /1000 / 60 /60) +"小时" );
        System.out.println("中华人民共和国成立了" + (interval /1000 /60 / 60 /24) + "天");

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922232.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号