Posted by joconner
on March 6, 2007 at 9:40 AM PST
The ability to set years, dates, and months got tossed in, and formatting abilities, and...something that should have been really lightweight became overweight, or maybe we should just say big boned.
java.util.Date represents a snapshot of time, independent of locale, timezones, etc. It does that pretty well. However, the little class got overworked early in its career. The ability to set years, dates, and months got tossed in, and formatting abilities, and...something that should have been really lightweight became overweight, or maybe we should just say big boned.
Almost immediately, Java engineers corrected the error by deprecating most of Date's API. Unfortunately, deprecation just means that the APIs are old, stale, and shouldn't be used. Usually, newer APIs improve the functionality that the deprecated methods should have provided from the beginning. Deprecated methods, however, never seem to actually go away. Regardless whether the methods are still available, you should take this simple message to heart: deprecated methods should not be used.
If you should never use deprecated methods, what can you do with a
Date object? I suppose the simple answer is "very little," but that sounds a bit like an arrogant reply, and of course that's not my intention. However, the truth is the truth. You can compare dates to see which precedes or follows another date, perform a clone operation, compare for equality, and well, that's about it. When it comes to anything really interesting regarding Dates, you'll have to get TimeZone and Calendar involved. That's ok too. I'm just trying to say that you shouldn't expect a lot of actual functionality from a
When you create a
Date object, you really only have two options if you don't get a
Date now = new Date();
Date otherTime = new Date(aLongValue);
The first option creates a snapshot of time right now at the time of instantiation. The second allows you to create a date from a long millisecond value, maybe a value that you had serialized or stored as a timestamp in a db.
I think the important thing to remember about
Date objects is that they are only a container for data...and only one piece of data at that. They hold a long value that represents the number of ms since Jan 1 1970 0:00 GMT.
More complex creation and manipulation of Dates will always involve another set of classes, typically a Calendar and TimeZone. Stay tuned for more on that...