Date vs TimeStamp vs calendar?

date, java

Solution

`java.sql.Timestamp` A thin wrapper around `java.util.Date` that allows the JDBC API to identify this as an SQL TIMESTAMP value.

If you check `java.sql.Timestamp` JavaDoc, it is very explicit that this class extends from `java.util.Date` (as `java.sql.Date` does). And in real world projects you must plain `java.util.Date` when storing the data in your database and mostly `java.sql.Timestamp` since it stores date and time value, while `java.sql.Date` just stores date value.

On the other hand, `java.util.Calendar` is abstract since there are more implementations of this apart from `java.util.GregorianCalendar`. If you see the code of `Calendar#getInstance` from HotSpot, you will see that it calls `createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT))`, and this method code uses 3 different calendars: `BuddhistCalendar`, `JapaneseImperialCalendar` and `GregorianCalendar`. This code is copied from JDK 7 source:

private static Calendar createCalendar(TimeZone zone,
                                       Locale aLocale) {
    Calendar cal = null;

    String caltype = aLocale.getUnicodeLocaleType("ca");
    if (caltype == null) {
        // Calendar type is not specified.
        // If the specified locale is a Thai locale,
        // returns a BuddhistCalendar instance.
        if ("th".equals(aLocale.getLanguage())
                && ("TH".equals(aLocale.getCountry()))) {
            cal = new BuddhistCalendar(zone, aLocale);
        } else {
            cal = new GregorianCalendar(zone, aLocale);
        }
    } else if (caltype.equals("japanese")) {
        cal = new JapaneseImperialCalendar(zone, aLocale);
    } else if (caltype.equals("buddhist")) {
        cal = new BuddhistCalendar(zone, aLocale);
    } else {
        // Unsupported calendar type.
        // Use Gregorian calendar as a fallback.
        cal = new GregorianCalendar(zone, aLocale);
    }

    return cal;
}

Now, why to work directly with `Calendar` instead of `GregorianCalendar`? Because you must work with abstract classes and interfaces when provided instead of working directly with implementations. This is better explained here: What does it mean to "program to an interface"?

Apart from this, if you will work with date and times, I recommend using a library like Joda-Time that already handles and solves lot of the problems with the current Java Date API and also provides methods to retrieve this date and times object in `java.util.Date` flavor.

Problem

I sometimes get confused by the different Date types in java and their practical usage. Here i am trying to summarize my understanding `java.sql.Date :-` A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value `java.sql.Timestamp :-` A thin wrapper around java.util.Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds I have seen most of the projects prefer Timestamp instead of date. I think the main reason for this is that Timestamp can hold the value till nano seconds precision whereas Data can hold till milli seconds. Correct? `Calendar :-` This class is designed for date manipulation for example :- for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.Though i dont know why this class is abstract when only one implementation exists i.e GregorianCalendar.

Original source

Related problems