Date : Java Glossary
home D words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
Date
Java has no standard classes for handling pure dates without times. It does have the following classes: These are among the worst bits of code ever written. You can do quite a bit with
// get time in milliseconds since 1970 Jan 1
long timestamp = System.currentTimeMillis();
If you you use pure longs, you don’t have to deal with timezones, leap years, months with variable number of days, regional differences, bulky objects, slow code and all the gotchas in the Date and Calendar classes. SQL can’t screw up a long. It surely can a SQLDate. Think hard to keep the bulk of your application dealing with simple longs. Only use Date, Calendar or CalendarFormat when you need to input or output human-readable dates/times.

There are a ton of surprises and bugs awaiting the unwary users of these classes.

lemonjava.util.Date Gotchas

The java.util.Date class is crawling with gotchas. It is a disgrace. It is the lemon of Java and deserves a giant string of raspberries. In JDK 1.2, Date has largely been deprecated, and replaced by something even more complicated, Calendar/ GregorianCalendar.

The key to understanding Date is that it is not a date class, but a timestamp class. Inside each date is stored the number of milliseconds since 1970-01-01 UTC. It does not record the timezone or the timestamp in local time. You choose the timezone when you display the date with code like this:

With Date, you can’t have a date without a time. Keep in mind that when you display the same Date with different timezones you will sometimes get 1999-12-14, and sometimes 1999-12-25.

In contrast GregorianCalendar objects do contain a TimeZone reference along with the date and time. Like Dates, they are initialised with the current date and time. Unfortunately, if they are not sure about the user’s timezone, the Sun classes quietly revert to GMT, and in early versions of GregorianCalendar, seem to revert to PST if you so much as breathe on them. DateFormats also have TimeZone that is similarly erratic. For code to work, your clients must have configured the TimeZone correctly in the OS. You can’t help them configure it.

One way out is to use my BigDate class which handles dates from 999,999 BC to 999,999 AD. Sun has deprecated most of the Date methods and replaced them with GregorianCalendar (which knows the TimeZone unlike Date). Date is still used for storing a date. GregorianCalendar would be far too bulky. GregorianCalendar has not nearly as many limitations as Date, it has got rid of those ridiculous 1900-based years, however it is obscenely complicated, still relies on the old Date class and maintains a lot of the Date lunacy such as 0-based months. Happily the documentation in JDK 1.2 is better, though ambiguity whether local or UTC parameters are wanted still plagues. Sun tends to be careless about documenting units of measure. For example in early JDK’s you never knew if TimeZone offsets were measured in milliseconds, seconds or minutes. getTimeZoneOffset returns minutes, but GregorianCalendar.get(Calendar.ZONE_OFFSET) returns milliseconds. See essay on Dates and Calendars .

Learning More

Sun’s Javadoc on the Date class : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 27,453. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/date.html J:\mindprod\jgloss\date.html