Java Reference
In-Depth Information
This year you will be 37 years old.
In 2011 your birthday will be on a Monday.
How It Works
You start by prompting for the day, month, and year for a date of birth to be entered through the keyboard
as integers. You then create a GregorianCalendar object corresponding to this date. Note the adjustment
of the month — the constructor expects January to be specified as 0 . You need a GregorianCalendar
object for today's date so you use the default constructor for this. To compute the age this year, you just
have to subtract the year of birth from this year, both of which you get from the GregorianCalendar
objects.
To get at the strings for the days of the week, you create a DateFormatSymbols object and call its
getWeekdays() method. This returns an array of eight String objects, the first of which is empty to
make it easy to index using day numbers from 1 to 7. The second element in the array contains "Sunday" .
You can also get the month names using the getMonths() method.
To display the day of the week for the date of birth, you call the get() method for the GregorianCal-
endar object birthdate and use the result to index the weekdays[] array. To determine the appropriate
text in the next two output statements, you use the after() and before() methods for Calendar objects
to compare today with the birthday date this year.
REGULAR EXPRESSIONS
You saw some elementary capability for searching strings when I discussed the String class in Chapter 4.
You have much more sophisticated facilities for analyzing strings using search patterns known as regular
expressions . Regular expressions are not unique to Java. Perl is perhaps better known for its support of reg-
ular expressions and C++ supports them too. Many word processors, especially on UNIX, support regular
expressions, and there are specific utilities for regular expressions, too. Many IDEs such as JCreator and
Microsoft Visual Studio also support regular expressions.
So what is a regular expression? A regular expression is simply a string that describes a pattern that
you use to search for matches within some other string. It's not simply a passive sequence of characters to
be matched, though. A regular expression is essentially a mini-program for a specialized kind of computer
called a state-machine . This isn't a real machine but a piece of software specifically designed to interpret a
regular expression and analyze a given string based on the operations implicit in a regular expression.
The regular expression capability in Java is implemented through two classes in the java.util.regex
package: the Pattern class, which defines objects that encapsulate regular expressions, and the Matcher
class, which defines an object that encapsulates a state-machine that can search a particular string using a
given Pattern object. The java.util.regex package also defines the PatternSyntaxException class,
which defines exception objects that are thrown when a syntax error is found when compiling a regular ex-
pression to create a Pattern object.
Using regular expressions in Java is basically very simple:
1. You create a Pattern object by passing a string containing a regular expression to the static com-
pile() method in the Pattern class.
2. You then obtain a Matcher object, which can search a given string for the pattern, by calling the
matcher() method for the Pattern object with the string that is to be searched as the argument.
Search WWH ::




Custom Search