3.25日报

日期问题

题目描述

小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在 1960 年 1 月 1 日至 2059 年 12 月 31 日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。

更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。

比如 02/03/04,可能是 2002 年 03 月 04 日、2004 年 02 月 03 日或 2004 年 03 月 02 日。

给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

输入输出样例

示例

输入

02/03/04

输出

2002-03-04
2004-02-03
2004-03-02

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class RiQiWenTi {
    public static void main(String[] args) throws ParseException {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        String d = scan.nextLine();
        String[] strs = d.split("/");
        Integer[] integers = new Integer[3];
        for (int i = 0; i < 3; i++) {
            integers[i] = Integer.valueOf(strs[i]);
        }
        String[] s012 = new String[3];
        Integer[] i012 = new Integer[3];
        s012[0] = strs[0];
        s012[1] = strs[1];
        s012[2] = strs[2];
        i012[0] = integers[0];
        i012[1] = integers[1];
        i012[2] = integers[2];
        String[] s201 = new String[3];
        Integer[] i201 = new Integer[3];
        s201[0] = strs[2];
        s201[1] = strs[0];
        s201[2] = strs[1];
        i201[0] = integers[2];
        i201[1] = integers[0];
        i201[2] = integers[1];
        String[] s210 = new String[3];
        Integer[] i210 = new Integer[3];
        s210[0] = strs[2];
        s210[1] = strs[1];
        s210[2] = strs[0];
        i210[0] = integers[2];
        i210[1] = integers[1];
        i210[2] = integers[0];
        String pd1 = panduan(s012, i012);
        String pd2 = panduan(s210, i210);
        String pd3 = panduan(s201, i201);
        String[] strings = new String[4];
        strings[0] = pd1;
        strings[1] = pd2;
        strings[2] = pd3;
        Date[] dates = new Date[4];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (pd1 != null) {
            dates[0] = sdf.parse(pd1);
        }
        if (pd2 != null) {
            dates[1] = sdf.parse(pd2);
        }
        if (pd3 != null) {
            dates[2] = sdf.parse(pd3);
        }
        for (int i = 0; i < 3; i++) {
            for (int j = i + 1; j < 3; j++) {
                if (dates[i] == null || dates[j] == null) {
                } else {
                    if (dates[i].compareTo(dates[j]) > 0) {
                        dates[3] = dates[i];
                        dates[i] = dates[j];
                        dates[j] = dates[3];
                        strings[3] = strings[i];
                        strings[i] = strings[j];
                        strings[j] = strings[3];
                    }else if (dates[i].compareTo(dates[j])==0){
                        strings[j]=null;
                    }
                }
            }
        }
        if (strings[0] != null) {
            System.out.println(strings[0]);
        }
        if (strings[1] != null) {
            System.out.println(strings[1]);
        }
        if (strings[2] != null) {
            System.out.println(strings[2]);
        }
        scan.close();
    }

    public static String panduan(String[] strings, Integer[] integers) {
        int[] max_days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int[] Rmax_days = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (integers[0] % 4 == 0) {
            if (integers[1] <= 12 && integers[1] > 0 && integers[2] > 0 && integers[2] <= Rmax_days[integers[1]]) {
                if (integers[0] < 60) {
                    return ("20" + strings[0] + "-" + strings[1] + "-" + strings[2]);

                } else {
                    return ("19" + strings[0] + "-" + strings[1] + "-" + strings[2]);
                }
            }
        } else {
            if (integers[1] <= 12 && integers[1] > 0 && integers[2] > 0 && integers[2] <= max_days[integers[1]]) {
                if (integers[0] < 60) {
                    return ("20" + strings[0] + "-" + strings[1] + "-" + strings[2]);
                } else {
                    return ("19" + strings[0] + "-" + strings[1] + "-" + strings[2]);
                }
            }
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值