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

时间:2023-03-09 06:04:52
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类从键盘获得输入。

 import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException; public class FormattedInput { public int readInt() throws InvalidUserInputException {
if (readToken() != StreamTokenizer.TT_NUMBER) {
throw new InvalidUserInputException("readInt() failed." + "Input data not numeric");
} if (tokenizer.nval > (double) Integer.MAX_VALUE || tokenizer.nval < (double) Integer.MIN_VALUE) {
throw new InvalidUserInputException("readInt() failed." + "Input outside range of type int");
} if (tokenizer.nval != (double) (int) tokenizer.nval) {
throw new InvalidUserInputException("readInt() failed." + "Input not an integer");
}
return (int) tokenizer.nval;
} public double readDouble() throws InvalidUserInputException {
if (readToken() != StreamTokenizer.TT_NUMBER) {
throw new InvalidUserInputException("readDouble() failed." + "Input data not numeric");
}
return tokenizer.nval;
} public String readString() throws InvalidUserInputException {
if (readToken() == StreamTokenizer.TT_WORD || ttype == '\"' || ttype == '\"') {
return tokenizer.sval;
} else {
throw new InvalidUserInputException("readString() failed." + "Input data is not a string");
}
}
// Plus methods to read various other data types... // Helper method to read the next token
private int readToken() {
try {
ttype = tokenizer.nextToken();
return ttype; } catch (IOException e) { // Error reading in nextToken()
e.printStackTrace();
System.exit(1); // End the program
}
return 0;
} // Object to tokenize input from the standard input stream
private StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
private int ttype; // Stores the token type code
}

然后需要InvalidUserInputException类

 public class InvalidUserInputException extends Exception {
public InvalidUserInputException() { } public InvalidUserInputException(String message) {
super(message);
} private static final long serialVersionUID = 9876L; }

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

 import java.util.GregorianCalendar;
import java.text.DateFormatSymbols;
import static java.util.Calendar.*; class TryCalendar {
public static void main(String[] args) {
FormattedInput in = new FormattedInput(); // Get the date of birth from the keyboard
int day = 0, month = 0, year = 0;
System.out.println("Enter your birth date as dd mm yyyy: ");
try {
day = in.readInt();
month = in.readInt();
year = in.readInt();
} catch(InvalidUserInputException e) {
System.out.println("Invalid input - terminating...");
System.exit(1);
} // Create birth date calendar -month is 0 to 11
GregorianCalendar birthdate = new GregorianCalendar(year, month-1,day);
GregorianCalendar today = new GregorianCalendar(); // Today's date // Create this year's birthday
GregorianCalendar birthday = new GregorianCalendar(
today.get(YEAR),
birthdate.get(MONTH),
birthdate.get(DATE)); int age = today.get(YEAR) - birthdate.get(YEAR); String[] weekdays = new DateFormatSymbols().getWeekdays(); // Get day names System.out.println("You were born on a " + weekdays[birthdate.get(DAY_OF_WEEK)]);
System.out.println("This year you " +
(birthday.after(today) ?"will be " : "are ") +
age + " years old.");
System.out.println("In " + today.get(YEAR) + " your birthday " +
(today.before(birthday)? "will be": "was") +
" on a "+ weekdays[birthday.get(DAY_OF_WEEK)] +".");
}
}