Introduction


From its earliest releases, Java included a class called Date designed for representing and operating upon dates. Its problems were that it was Anglocentric like much of Java 1.0 and that its dates began with the Unix time epoch: January 1, 1970. The year was an integer whose minimum value 70 represented 1970, so 99 was 1999, 100 was 2000, and so on. This led to the problem that those of us ancient enough to have been born before that venerable year of 1970 in the history of computing the time when Unix was invented found ourselves unable to represent our birthdates, and this made us grumpy and irritable.

The Anglocentricity and 1970-centricity were partly vanquished with Java 1.1. A new class, Calendar , was devised, with hooks for representing dates in any date scheme such as the Western (Christian) calendar, the Hebrew calendar, the Islamic calendar, the Chinese calendar, and even Star Trek Star Dates. Unfortunately, there wasn't enough time to implement any of these. In fact, only the GregorianCalendar class appears in Java 1.1, and subsequent Java versions have done little to solve the problem (though 1.2 did repair the Date class to allow it to represent years before 1970.) You may have to go to other sources to get additional calendar classes; one source is listed in Recipe Recipe 6.3.

The Calendar class can represent any date, BC or AD, in the Western calendar. A separate Java int variable, with 32 bits of storage, is allocated for each item such as year, month, day, and so on. Years are signed, with negative numbers meaning before the calendar epoch and positive numbers after it. The term epoch means the beginning of recorded time. In the Western world, our calendar epoch is the year 1, representing the putative birth year of Jesus Christ. This is such an important event in Western society that the years before it are called Before Christ or BC, and dates since then are called . . . well, not After Christ, but the Latin anno domini, "in the year of our Lord." Because that takes too long to say and write, we use the acronym AD, thus proving that computerists take no blame whatsoever for inventing the use of acronyms. In the modern spirit of political correctness, these terms have been renamed to BCE (Before Common Era) and CE (Common Era), but to most English speakers born before about 1980, they will always be BC and AD. The GregorianCalendar class, intended to represent Western or Christian dates, also uses BC and AD.

Where was I? Oh yes, Java. As ints in Java are 32 bits, that allows 2^31, or 2,147,483,648, years. Let's say roughly two billion years. I, for one, am not going to worry about this new Y2B menace even if I'm still around, I'm sure they'll have gone to a 64-bit integer by then.

Fortunately, in JDK 1.2, the Date class was changed to use long values, and it can now represent a much wider range of dates. And what about the DateFormat class? Well, it provides a great deal of flexibility in the formatting of dates. Plus, it's bidirectional it can parse dates too. We'll see it in action in Recipes Recipe 6.2 and Recipe 6.5.

Note also that some of these classes are in package java.text, while others are in java.util. Package java.text contains classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages, while java.util contains the collections framework and legacy collection classes (see Chapter 7), event model, date and time facilities, internationalization, and miscellaneous utility classes. You'll need imports from both packages in most date-related programs.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net