Java基础之一组有用的类——使用公历日历(TryCalendar)

控制台程序。

公历是西方使用的日历,用GregorianCalendar类的对象来表示。GregorianCalendar对象封装了时区信息、日期和时间数据。GregorianCalendar对象有7个构造函数,默认构造函数用当前日期和时间在计算机所在的默认地点创建了日历,其他构造函数则指定了年份、月份、日期、小时、分钟和秒。默认构造函数适用于大多数情况。默认构造函数为:

GregorianCalendar calendar=new GregorianCalendar();

这个对象被设置为当前时间,调用它的getTime()方法可以把这个对象当作Date对象来访问:

Date now =calendar.getTime();

使用如下任意构造函数可以创建封装了特定日期和/或时间的GregorianCalendar对象:

GregorianCalendar(int year, int month, int day)

GregorianCalendar(int year, int month, int day, int hour, int minute)

GregorianCalendar(int year, int month, int day, int hour, int minute, int second)

day参数是月份中的天,所以值可以是1到28、29、30或31,这取决与月份以及是否为闰年。month参数的值是基于0的,所以表示一月的值是0,表示12月的值是11.

首先需要FormatInput类从键盘获得输入。

 1 import java.io.StreamTokenizer;
 2 import java.io.BufferedReader;
 3 import java.io.InputStreamReader;
 4 import java.io.IOException;
 5 
 6 public class FormattedInput {
 7 
 8   public int readInt() throws InvalidUserInputException {
 9     if (readToken() != StreamTokenizer.TT_NUMBER) {
10       throw new InvalidUserInputException("readInt() failed." + "Input data not numeric");
11     }
12 
13     if (tokenizer.nval > (double) Integer.MAX_VALUE || tokenizer.nval < (double) Integer.MIN_VALUE) {
14       throw new InvalidUserInputException("readInt() failed." + "Input outside range of type int");
15     }
16 
17     if (tokenizer.nval != (double) (int) tokenizer.nval) {
18       throw new InvalidUserInputException("readInt() failed." + "Input not an integer");
19     }
20     return (int) tokenizer.nval;
21   }
22 
23   public double readDouble() throws InvalidUserInputException {
24     if (readToken() != StreamTokenizer.TT_NUMBER) {
25       throw new InvalidUserInputException("readDouble() failed." + "Input data not numeric");
26     }
27     return tokenizer.nval;
28   }
29 
30   public String readString() throws InvalidUserInputException {
31     if (readToken() == StreamTokenizer.TT_WORD || ttype == '"' || ttype == '"') {
32       return tokenizer.sval;
33     } else {
34       throw new InvalidUserInputException("readString() failed." + "Input data is not a string");
35     }
36   }
37   // Plus methods to read various other data types...
38 
39   // Helper method to read the next token
40   private int readToken() {
41     try {
42       ttype = tokenizer.nextToken();
43       return ttype;
44 
45     } catch (IOException e) {                                          // Error reading in nextToken()
46       e.printStackTrace();
47       System.exit(1);                                                  // End the program
48     }
49     return 0;
50   }
51 
52   // Object to tokenize input from the standard input stream
53   private StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
54   private int ttype;                                                   // Stores the token type code
55 }

然后需要InvalidUserInputException类

 1 public class InvalidUserInputException extends Exception {
 2   public InvalidUserInputException() {  }
 3 
 4   public InvalidUserInputException(String message) {
 5     super(message);
 6   }
 7 
 8   private static final long serialVersionUID = 9876L;
 9 
10 }

最后是主程序,这个例子用于推断用户出生时的重要信息。

 1 import java.util.GregorianCalendar;
 2 import java.text.DateFormatSymbols;
 3 import static java.util.Calendar.*;
 4 
 5 class TryCalendar {
 6   public static void main(String[] args) {
 7     FormattedInput in = new FormattedInput();
 8 
 9     // Get the date of birth from the keyboard
10     int day = 0, month = 0, year = 0;
11     System.out.println("Enter your birth date as dd mm yyyy: ");
12     try {
13       day = in.readInt();
14       month = in.readInt();
15       year = in.readInt();
16     } catch(InvalidUserInputException e) {
17       System.out.println("Invalid input - terminating...");
18       System.exit(1);
19     }
20 
21     // Create birth date calendar -month is 0 to 11
22     GregorianCalendar birthdate = new GregorianCalendar(year, month-1,day);
23     GregorianCalendar today = new GregorianCalendar();                 // Today's date
24 
25     // Create this year's birthday
26     GregorianCalendar birthday = new GregorianCalendar(
27                                         today.get(YEAR),
28                                         birthdate.get(MONTH),
29                                         birthdate.get(DATE));
30 
31     int age = today.get(YEAR) - birthdate.get(YEAR);
32 
33     String[] weekdays = new DateFormatSymbols().getWeekdays();         // Get day names
34 
35     System.out.println("You were born on a " + weekdays[birthdate.get(DAY_OF_WEEK)]);
36     System.out.println("This year you " +
37                         (birthday.after(today)   ?"will be " : "are ") +
38                         age + " years old.");
39     System.out.println("In " + today.get(YEAR) + " your birthday " +
40                        (today.before(birthday)? "will be": "was") +
41                        " on a "+ weekdays[birthday.get(DAY_OF_WEEK)] +".");
42   }
43 }
原文地址:https://www.cnblogs.com/mannixiang/p/3439590.html