Workshop: Watching the Clock

This hour's workshop gives you another look at each of the conditional tests you can use in your programs. For this project, you will use Java's built-in timekeeping feature, which keeps track of the current date and time, and present this information in sentence form. Run the word processor you're using to create Java programs and give a new document the name ClockTalk.java. This program is long, but most of it consists of long conditional statements. Type the full text of Listing 7.3 into the word processor and save the file as ClockTalk.java when you're done.

Listing 7.3. The ClockTalk Program
 1: import java.util.*;
 2:
 3: class ClockTalk {
 4: public static void main(String[] arguments) {
 5: // get current time and date
 6: Calendar now = Calendar.getInstance();
 7: int hour = now.get(Calendar.HOUR_OF_DAY);
 8: int minute = now.get(Calendar.MINUTE);
 9: int month = now.get(Calendar.MONTH) + 1;
10: int day = now.get(Calendar.DAY_OF_MONTH);
11: int year = now.get(Calendar.YEAR);
12:
13: // display greeting
14: if (hour < 12) {
15: System.out.println("Good morning.\n");
16: } else if (hour < 17) {
17: System.out.println("Good afternoon.\n");
18: } else {
19: System.out.println("Good evening.\n");
20: }
21:
22: // begin time message by showing the minutes
23: System.out.print("It's");
24: if (minute != 0) {
25: System.out.print(" " + minute + " ");
26: System.out.print( (minute != 1) ? "minutes" :
27: "minute");
28: System.out.print(" past");
29: }
30:
31: // display the hour
32: System.out.print(" ");
33: System.out.print( (hour > 12) ? (hour - 12) : hour );
34: System.out.print(" o'clock on ");
35:
36: // display the name of the month
37: switch (month) {
38: case 1:
39: System.out.print("January");
40: break;
41: case 2:
42: System.out.print("February");
43: break;
44: case 3:
45: System.out.print("March");
46: break;
47: case 4:
48: System.out.print("April");
49: break;
50: case 5:
51: System.out.print("May");
52: break;
53: case 6:
54: System.out.print("June");
55: break;
56: case 7:
57: System.out.print("July");
58: break;
59: case 8:
60: System.out.print("August");
61: break;
62: case 9:
63: System.out.print("September");
64: break;
65: case 10:
66: System.out.print("October");
67: break;
68: case 11:
69: System.out.print("November");
70: break;
71: case 12:
72: System.out.print("December");
73: }
74:
75: // display the date and year
76: System.out.println(" " + day + ", " + year + ".");
77: }
78: }


Save the file when you're done, and attempt to compile it (JDK users can enter javac ClockTalk.java at the command line). Correct any typos that cause error messages to occur during the attempted compilation. After the program compiles correctly, look it over before reading more about the program. See whether you can get a good idea about what is taking place in each of its sections and how the conditional tests are being used. With the exception of Lines 6–11, the ClockTalk program contains material that has been covered up to this point. After a series of variables are set up to hold the current date and time, a series of if or switch conditionals are used to determine what information should be displayed. This program contains several uses of System.out.println() and System.out.print() to display strings. Lines 6–11 refer to a Calendar variable called now. The Calendar variable type is capitalized, just as String is capitalized in a program that uses strings. The reason for the capitalization is that Calendar is an object. You'll learn how to create and work with objects during Hour 10, "Creating Your First Object." For this hour, focus on what's taking place in those lines rather than how it's happening. The ClockTalk program is made up of the following sections:

  • Line 1 enables your program to use a class that is needed to track the current date and time: java.util.Calendar.
  • Lines 3–4 begin the ClockTalk program and its main() statement block.
  • Line 6 creates a Calendar object called now that contains the current date and time of your system. The now object will change each time you run this program (unless, of course, the physical laws of the universe are altered and time stands still).
  • Lines 7–11 create variables to hold the hour, minute, month, day, and year. The values for these variables are pulled from the Calendar object, which is the storehouse for all of this information. These variables are used in the subsequent sections as the program displays information.
  • Lines 14–20 display one of three possible greetings: Good morning., Good afternoon., or Good evening. The greeting to display is selected based on the value of the hour variable.
  • Lines 23–29 display the current minute along with some accompanying text. First, the text It's is displayed in Line 23. If the value of minute is equal to 0, Lines 25–28 are ignored because of the if statement in Line 24. This statement is necessary because it would not make sense for the program to tell someone that it's 0 minutes past an hour. Line 25 displays the current value of the minute variable. A ternary operator is used in Lines 26–27 to display either the text minutes or minute, depending on whether minute is equal to 1. Finally, in Line 28 the text past is displayed.
  • Lines 32–34 display the current hour by using another ternary operator. This ternary conditional statement in Line 33 causes the hour to be displayed differently if it is larger than 12, which prevents the computer from stating times like "15 o'clock."
  • Lines 37–73, almost half of the program, are a long switch statement that displays a different name of the month based on the integer value stored in the month variable.
  • Line 76 finishes off the display by showing the current date and the year.
  • Lines 77–78 close out the main() statement block and then the entire ClockTalk program.

When you run this program, the output should resemble the following code, with changes based on the current date and time. For example, if the program was run on 6/26/2005 at 11:33 p.m., it would display the following text:

Good evening.
It's 33 minutes past 11 o'clock on June 26, 2005.


Run the program several times to see how it keeps up with the clock. If the time doesn't match the time on your computer, the Java interpreter might be using the wrong time zone to determine the current time. When the interpreter does not know the default time zone to use, it uses Greenwich Time instead. The following statements set the current time zone to U.S. Eastern time:

TimeZone tz = TimeZone.getTimeZone("EST");
TimeZone.setDefault(tz);


The setDefault() method should be used before calendar or any other date-related items are created. The first statement creates a TimeZone object called tz. The text EST is sent as an argument to the getTimeZone() method, and this causes TimeZone to be set up for Eastern Time. The second statement sets the time zone by calling the setDefault () method of the TimeZone class. If you're having trouble finding the right time zone arguments, the following statements display all valid zones recognized by Java on your system:

String[] ids = TimeZone.getAvailableIDs();
for (int i = 0; i < ids.length; i++)
 System.out.println(ids[i].toString());


By the Way

The ClockTalk program uses the Gregorian calendar system that has been used throughout the Western world for many years to determine the date and time. It was introduced in 1582 when Pope Gregory XIII moved the Julian calendar system forward 10 days—turning Oct. 5, 1582, into Oct. 15, 1582. This was needed because the calendar was moving out of alignment with the seasons due to discrepancies in the Julian system (which had to be small comfort to anyone who had a birthday on October 6 through October 14). Java also supports the use of alternate calendar systems—if you're looking for a class that supports the Arabic, Buddhist, Japanese, or Hebrew calendar systems, visit the International Components for Unicode for Java site at the web address http://www-306.ibm.com/software/globalization/icu.


      
Comments