Re: Convert from java.sql.Date to GregorianCalendar

From: P.Hill (goodhill_at_xmission.com)
Date: 01/30/04


Date: Thu, 29 Jan 2004 16:08:54 -0700

TT (Tom Tempelaere) wrote:
>>>A Calendar is a variation of a strategy pattern, it is the thing that
>>>contains the algorthms for taking day 28002 (or whatever the internal
>>>value for a day it) and changing it into something with Year-Month-Day
>>>components. It is very large compared to a Date.

>>I thought about using GregorianCalendar objects for
>>dates. Plus, no real conversion is necessary if the client (UI) needs a
>>gregoriancalendar.

You'll find that JSPs/JSTL/Struts/Swing etc. deal in Dates not Calendars.
Sending around a Calendar is like sending around a sort routine, when
what you really want to send around is a sorted list.
USE A DATE!

Any time you have a binary Date object you have to ask what time zone
you wish to display the value in (plus you sometimes need to worry about
language). Date does not have such features, so you create a GregorianCalendar
push the date in and ask for some fields back out.
But often you use SimpleDateFormat and just ask for a formatted string, which
will deal with Calendar for you.

>>Can you tell me how TimeStamp differs from Date for instance?

As the API says, it is a Date with a nanosecond field. The nanoseconds
replaces the milliseconds. It also is used to Identify to
the JDBC API that you have something which represents a TimeStamp.
Again, read the API.

Why do you think you want to use it in your program;
You might define you DB to contain such a thing, but you can always
ask the JDBC layer for a Date object and send that around.

Why is Date mostly deprecated? It is now intended to _hold_ a
binary date. Because ettting month day and year requires knowledge of
timezone and language, so all that knowledge is over in Calendar.
Does that make sense? If not just use it that way anyway.
Dates for in and out of GUIs, DBs and transport protocols (
for javaMail uses Dates also), while Calendar are
used when you or DateFormat wants to separate things into
separate month, day, year, etc. fields.
Does that make sense?

-Paul



Relevant Pages

  • Re: Possible bug in Calendar
    ... It may be a concern of application user-interface design, but that's a whole different kettle of fish. ... who use non-Gregorian calendars for civil purposes, by requiring that the API dictate everything in Gregorian, you've essentially said "screw you" to said countries. ... There can be a translation class used to interface to the user using whatever means of measuring and naming times that they prefer, of course, but the core should be something simple and world-standard, like a Gregorian calendar or seconds since the epoch or whatever. ...
    (comp.lang.java.programmer)
  • Re: question about the calendar class
    ... set(int year, int month, int date) ... Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH * ... There is nothing in the API about this method throwing a NPE exception. ...
    (comp.lang.java.help)
  • Re: question about the calendar class
    ... set(int year, int month, int date) ... Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH * ... There is nothing in the API about this method throwing a NPE exception. ...
    (comp.lang.java.help)
  • Re: What a mess: Date, milliseconds, GregorianCalendar
    ... Java's date/time API are confusing, inconsistent, and sometimes ... You import Date and Calendar twice, but don't explicitly use either one. ... the last (GregorianCalendar) import. ...
    (comp.lang.java.programmer)
  • Re: creating multiple .pst file for multiple outlook 2000 users
    ... I'm would like for each of the users to have a separate calendar. ... > unsolicited mail sent to my personal account will be deleted without ... PROFILES, PEROPERTIES, DATA FILES. ...
    (microsoft.public.office.misc)