UT源码105032014119

 1 import java.text.SimpleDateFormat;
 2 import java.util.Calendar;
 3 import java.util.Scanner;
 4 
 5 /*NextDate函数问题
 6 
 7 NextDate函数说明一种复杂的关系,即输入变量之间逻辑关系的复杂性
 8 
 9 NextDate函数包含三个变量month、day和year,函数的输出为输入日期后一天的日期。 要求输入变量month、day和year均为整数值,并且满足下列条件:
10 
11 条件1 1≤ month ≤12 否则输出,月份超出范围
12 
13 条件2 1≤ day ≤31 否则输出,日期超出范围
14 
15 条件3 1912≤ year ≤2050 否则输出:年份超出范围
16 
17 String nextdate(int m,int d,int y)
18 
19 注意返回值是字符串。
20 
21 程序要求:
22 
23 1)先显示“请输入日期”
24 
25 2)不满足条件1,返回:“月份超出范围”;不满足条件2,返回:“日期超出范围”;不满足条件3,返回:“年份超出范围”;如果出现多个不满足,以最先出现不满足的错误返回信息。
26 
27 3)条件均满足,则输出第二天的日期:格式“****年**月**日”(如果输入2050年12月31日,则正常显示2051年1月1日*/
28 public class MyDate {
29     public static String nextdate(int month, int day, int year) {
30         if (1 <= month && month <= 12)
31             if (1 <= day && day <= 31)
32                 if (1912 <= year && year <= 2050) {
33                     SimpleDateFormat sFormat = new SimpleDateFormat("yyyyMMdd");
34                     sFormat.setLenient(false);
35                     try {
36                         Calendar c = Calendar.getInstance();
37                         c.setTime(sFormat.parse("" + year + String.format("%02d", month) + String.format("%02d", day)));
38                         c.add(Calendar.DATE, 1);
39                         return c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH) + 1) + "月" + c.get(Calendar.DATE)
40                                 + "日";
41                     } catch (Exception e) {
42                         return "日期不存在";
43                     }
44                 } else
45                     return "年份超出范围";
46             else
47                 return "日期超出范围";
48         else
49             return "月份超出范围";
50     }
51 
52     public static void main(String[] args) {
53         String input = "";
54         Scanner scan = new Scanner(System.in);
55         while (true) {
56             System.out.print("请输入日期[日期格式yyyy MM dd]");
57             input = scan.nextLine();
58             String[] buf = input.split("\\s+");
59             if (input.equals(""))
60                 break;
61             else if (buf.length == 3) {
62                 try {
63                     int month = Integer.valueOf(buf[1]);
64                     int date = Integer.valueOf(buf[2]);
65                     int year = Integer.valueOf(buf[0]);
66                     System.out.println(nextdate(month, date, year));
67                 } catch (Exception e) {
68                     System.out.println("日期格式错误");
69                     continue;
70                 }
71             } else {
72                 System.out.println("日期格式错误");
73                 continue;
74             }
75         }
76         System.out.println("谢谢使用,再见~");
77         scan.close();
78     }
79 }

 

posted @ 2017-03-10 14:43  景诺晨曦  阅读(377)  评论(0)    收藏  举报