第8届蓝桥杯大赛个人赛省赛(软件类)真题\Java语言B组\7

题目描述:


标题:日期问题

小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。  

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

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

输入
----
一个日期,格式是"AA/BB/CC"。  (0 <= A, B, C <= 9)  

输入
----
输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。多个日期按从早到晚排列。  

样例输入
----
02/03/04  

样例输出
----
2002-03-04  
2004-02-03  
2004-03-02  

资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 1000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
不要使用package语句。不要使用jdk1.7及以上版本的特性。
主类的名字必须是:Main,否则按无效代码处理。


直接上代码:

package wiki.zimo.exam08;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Demo07 {
	public static void main(String[] args) {
		String dateStr = new Scanner(System.in).nextLine();
		String[] split = dateStr.split("/");
		
		int a = Integer.valueOf(split[0]);
		int b = Integer.valueOf(split[1]);
		int c = Integer.valueOf(split[2]);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Set<String> set = new HashSet<>();
		
		// 日期范围是:1960年1月1日至2059年12月31日
		Date start = new Date("1960/01/01");
        Date end = new Date("2059/12/31");
        
		// a可以是年或者月或者日,b可以是月或者日,c可以是日或者年
		
		// 1.a是年,b是月,c是日
		{
			Date date = new Date();
			int year = adjustYear(a);
			int month = adjustMonth(b);
			if (year >= 0 && month >= 0) {
				date.setYear(year);
				date.setMonth(month);
			}
			int day = adjustDay(date,c);

			if (year >= 0 && month >= 0 && day >= 0) {
				if (!date.before(start) && !date.after(end)) {
					set.add(sdf.format(date));
				}
			}
		}
		

		// 2.a是月,b是日,c是年
		{
			Date date = new Date();
			int year = adjustYear(c);
			int month = adjustMonth(a);
			if (year >= 0 && month >= 0) {
				date.setYear(year);
				date.setMonth(month);
			}
			int day = adjustDay(date,b);
			
			if (year >= 0 && month >= 0 && day >= 0) {
				if (!date.before(start) && !date.after(end)) {
					set.add(sdf.format(date));
				}
			}
		}
		
		
		// 3.a是日,b是月,c是年
		{
			Date date = new Date();
			int year = adjustYear(c);
			int month = adjustMonth(b);
			if (year >= 0 && month >= 0) {
				date.setYear(year);
				date.setMonth(month);
			}
			int day = adjustDay(date, a);
			if (year >= 0 && month >= 0 && day >= 0) {
				if (!date.before(start) && !date.after(end)) {
					set.add(sdf.format(date));
				}
			}
		}
		
		List<String> list = new ArrayList<>(set);
		Collections.sort(list);
		
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}

	public static int adjustDay(Date date, int c) {
		if (c >= 1 && c <= 31) {
			date.setDate(c);
		}
		if (date.getDate() == c) {
			return c;
		}
		return -1;
	}

	public static int adjustYear(int year) {
		if (year >=0 && year <= 59) {
			return  year + 2000 - 1900;
		}else if (year >= 60 && year <= 99) {
			return year + 1900 - 1900;
		}
		return -1;
	}
	
	public static int adjustMonth(int month) {
		if (month >= 1 && month <= 12) {
			return month - 1;
		}
		return -1;
	}
	
}
发布了69 篇原创文章 · 获赞 50 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_36737934/article/details/88722902