The Source for Java Technology Collaboration
User: Password:



John O'Conner

John O'Conner's Blog

Dating 101

Posted by joconner on March 06, 2007 at 09:40 AM | Comments (7)

The 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 Date object.

When you create a Date object, you really only have two options if you don't get a Calendar involved:


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


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • To be honest,

    The after(), before() and equals() method in the java.util.Date is very useful. However, what do I supposed to do if the basic functions of a "date container" like: getHour(), getYear() are deprecated.

    Even I can convert a "long value that represents the number of ms since Jan 1 1970 0:00 GMT" to a Date object, what can I do if i can't get the Hour, Year..etc from it....

    Posted by: fcmmok on March 06, 2007 at 10:46 AM

  • There are date comparison functions here

    Posted by: niallp on March 06, 2007 at 11:01 AM

  • In this discussion, it would be interesting to touch upon java.sql.Date (and other time-related classes in JDBC), because it's a messy overloading of the Date class. Furthermore, I've had no end of trouble with different JDBC driver's (and versions thereof) regarding interpretation of calendars/timezones. So, if you're going to look into this more,it'd be a useful subtopic to touch upon...
    - Chris

    Posted by: chris_e_brown on March 06, 2007 at 11:18 PM

  • Chris,

    I would be interested in your java.sql.Date issues as we look at areas to improve the JDBC API.

    Thank you,
    Lance

    Posted by: lancea on March 07, 2007 at 10:38 AM

  • However, what do I supposed to do if the basic functions of a "date container" like: getHour(), getYear() are deprecated.


    You are supposed to use a Calendar. The API documentation of Date even points to the corresponding Calendar methods.


    Even I can convert a "long value that represents the number of ms since Jan 1 1970 0:00 GMT" to a Date object, what can I do if i can't get the Hour, Year..etc from it....


    Use a Calendar. Two more lines of code.

    Date date = ...
    Calender calendar = new GregorianCalendar();
    calendar.setTime(date);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int year4 = calendar.get(Calendar.YEAR);

    Instead of


    Date date = ...
    int hour = date.getHour();
    int year4 = date.getYear() + 1900;

    Posted by: ewin on March 07, 2007 at 01:37 PM

  • @ewin

    u is a funny guy. Can u read.?! You is completely off the topic here.

    I'm asking for the functionalities of java.util.Date not Calendar.

    Of course, I know that the Calendar class can do what that.
    This is just basic requirement of a so call "Java Developer"

    We are discussing about Date not Calendar. Learn how to read and be more clever next time.

    Posted by: fcmmok on March 12, 2007 at 06:56 AM

  • @fcmmok, I think ewin answered your question quite well. The point is that you actually don't have much functionality with a Date object. You should not use its deprecated methods, so the solution for retrieving those date elements is not the Date object itself; it's Calendar. My next blog post describes this too.
    I think your sarcasm is unjustified. I'm glad you've read and commented here, but you might be missing my main point for this blog. Although this blog discusses Date, the final conclusion should be that you cannot use Date for the purposes you describe.

    Posted by: joconner on March 12, 2007 at 08:51 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds