Chapter 6. Dates and Times


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 ...

Get Java Cookbook, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.