Convert from java.sql.Date to GregorianCalendar

Discussion in 'Java' started by TT \(Tom Tempelaere\), Jan 29, 2004.

  1. Hi,

    How do I convert from java.sql.Date to GregorianCalendar?

    Thanks,
    Tom Tempelaere
    TT \(Tom Tempelaere\), Jan 29, 2004
    #1
    1. Advertising

  2. TT \(Tom Tempelaere\)

    P.Hill Guest

    TT (Tom Tempelaere) wrote:
    > How do I convert from java.sql.Date to GregorianCalendar?


    Calendar.setTime( Date );

    Just an old weird API name to confuse you.

    -Paul
    P.Hill, Jan 29, 2004
    #2
    1. Advertising

  3. "P.Hill" <> wrote in message
    news:bvb7s9$1bc$...
    > TT (Tom Tempelaere) wrote:
    > > How do I convert from java.sql.Date to GregorianCalendar?

    >
    > Calendar.setTime( Date );
    >
    > Just an old weird API name to confuse you.
    >
    > -Paul


    So I should construct default GregorianCalendar, and then call setTime?

    Date dt = /*...*/;
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime( dt.getTime() );

    I should probably put this in a conversion function.

    Tom.
    TT \(Tom Tempelaere\), Jan 29, 2004
    #3
  4. "P.Hill" <> wrote in message
    news:bvb7s9$1bc$...
    > TT (Tom Tempelaere) wrote:
    > > How do I convert from java.sql.Date to GregorianCalendar?

    >
    > Calendar.setTime( Date );
    >
    > Just an old weird API name to confuse you.
    >
    > -Paul



    I what date format would you supply dates to a client (in a client/server
    model)? Calendar or Date?

    Tom.
    TT \(Tom Tempelaere\), Jan 29, 2004
    #4
  5. "P.Hill" <> wrote in message
    news:bvb7s9$1bc$...
    > TT (Tom Tempelaere) wrote:
    > > How do I convert from java.sql.Date to GregorianCalendar?

    >
    > Calendar.setTime( Date );
    >
    > Just an old weird API name to confuse you.
    >
    > -Paul



    Does this work with TimeStamp (so that it doesn't lose the nano's)?

    Tom.
    TT \(Tom Tempelaere\), Jan 29, 2004
    #5
  6. TT \(Tom Tempelaere\)

    P.Hill Guest

    TT (Tom Tempelaere) wrote:
    > "P.Hill" <> wrote in message
    >>
    >>Calendar.setTime( Date );
    >>

    > So I should construct default GregorianCalendar, and then call setTime?
    >
    > Date dt = /*...*/;
    > GregorianCalendar gc = new GregorianCalendar();
    > gc.setTime( dt.getTime() );
    >


    You should probabely read the API docs, or at least read the message to
    which you are replying.

    gc.setTime takes a ___Date___ not a millisecond time value.

    If you have this thing that know about putting together and taking apart
    dates then to set it to a new date you need to be able to push
    some token that presents a moment in time into it. That is
    what "setTime( Date date )" is for.

    -Paul
    P.Hill, Jan 29, 2004
    #6
  7. TT \(Tom Tempelaere\)

    P.Hill Guest

    TT (Tom Tempelaere) wrote:

    > I what date format would you supply dates to a client (in a client/server
    > model)? Calendar or Date?


    If you are NOT dealing with issues of displaying time in timezone other
    than that of the client, and you just need to display a time in the timezone of
    the user then I would use the much more compact binary representation of date
    and time, the java.util.Date.

    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 would suggest you also look into SimpleDateFormat which takes
    a date and makes a String. This is usually what you'll need in
    a client application.

    -Paul
    P.Hill, Jan 29, 2004
    #7
  8. TT \(Tom Tempelaere\)

    P.Hill Guest

    TT (Tom Tempelaere) wrote:

    > Does this work with TimeStamp (so that it doesn't lose the nano's)?


    What do you mean, does it work?
    I think you need to learn to understand objects and the docs for those
    objects.

    Try:
    http://java.sun.com/j2se/1.4.2/docs/api/

    Can you find some method that allows you to send a TimeStamp into
    a calendar?
    Can you find any discussion of nanoseconds in Calendar or DateFormat?
    Are you really interested in making a String out of a timestamp?
    Why? What are you going to do with it?

    -Paul
    P.Hill, Jan 29, 2004
    #8
  9. TT (Tom Tempelaere) wrote:
    > I what date format would you supply dates to a client (in a client/server
    > model)? Calendar or Date?


    As integer timestamps, where 0 == 1970-01-01 00:00H GMT. Probably
    amended with a timezone identification of the place of origin, solely to
    be used for display purposes, but never ever a timezone-relative date value.

    /Thomas
    Thomas Weidenfeller, Jan 29, 2004
    #9
  10. TT \(Tom Tempelaere\)

    P.Hill Guest

    Thomas Weidenfeller wrote:

    > TT (Tom Tempelaere) wrote:
    >> I what date format would you supply dates to a client

    >
    > As integer timestamps, where 0 == 1970-01-01 00:00H GMT.


    Which is what a java.util.Date is counting by internally.

    > Probably
    > amended with a timezone identification of the place of origin, solely to
    > be used for display purposes, but never ever a timezone-relative date
    > value.


    Tom (the OP), If you need TZ information, you could build yourself
    and object that is just a java.util.Date and a appropriate TZ ID as described
    by Tom W.

    If you expect the user to display all times in the users TZ -- because either
    everything happens in one TZ or the client local TZ is the logical choice --
    just talk with the Date object. JDBC will put dates into DBs appropriately,
    DateFormat will create strings for you. If you need to do you own
    calendar calculations, then use a Gregorian Calendar.

    -Paul
    P.Hill, Jan 29, 2004
    #10
  11. "P.Hill" <> wrote in message
    news:bvbetm$47h$...
    > TT (Tom Tempelaere) wrote:
    >
    > > I what date format would you supply dates to a client (in a

    client/server
    > > model)? Calendar or Date?

    >
    > If you are NOT dealing with issues of displaying time in timezone other
    > than that of the client, and you just need to display a time in the

    timezone of
    > the user then I would use the much more compact binary representation of

    date
    > and time, the java.util.Date.
    >
    > 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 would suggest you also look into SimpleDateFormat which takes
    > a date and makes a String. This is usually what you'll need in
    > a client application.
    >
    > -Paul


    The specific server routine returns a vector of objects (history). The
    objects have a few dates as members, and these should be easily convertible
    to strings (displaying), and easily to be used for comparison (post
    selection operations - server side or client side) etc.
    Since java.util.Date is 75% deprecated, and those include the members I need
    to extract the values, I thought about using GregorianCalendar objects for
    dates. Plus, no real conversion is necessary if the client (UI) needs a
    gregoriancalendar. To be honest I haven't done too much UI-coding lately,
    and I wouldn't know the best format to supply the dates.
    I'm relatively new to java, and I am not used to finding so many date/time
    related classes in a library.
    Can you tell me how TimeStamp differs from Date for instance?

    Thanks,
    Tom.
    TT \(Tom Tempelaere\), Jan 29, 2004
    #11
  12. "P.Hill" <> wrote in message
    news:bvbf5b$4bg$...
    >
    >
    > TT (Tom Tempelaere) wrote:
    >
    > > Does this work with TimeStamp (so that it doesn't lose the nano's)?

    >
    > What do you mean, does it work?
    > I think you need to learn to understand objects and the docs for those
    > objects.
    >
    > Try:
    > http://java.sun.com/j2se/1.4.2/docs/api/
    >
    > Can you find some method that allows you to send a TimeStamp into
    > a calendar?


    TimeStamp derives from java.util.Date. I just wondered if it took
    nanoseconds into account. Guess not.

    > Can you find any discussion of nanoseconds in Calendar or DateFormat?
    > Are you really interested in making a String out of a timestamp?
    > Why? What are you going to do with it?


    My colleague is using that, I wouldn't know why either.

    Tom.

    > -Paul
    >
    TT \(Tom Tempelaere\), Jan 29, 2004
    #12
  13. "TT (Tom Tempelaere)" <_N_> wrote in
    message news:M0eSb.12192$-ops.be...
    > "P.Hill" <> wrote in message
    > news:bvbetm$47h$...
    > > TT (Tom Tempelaere) wrote:
    > >
    > > > I what date format would you supply dates to a client (in a

    > client/server
    > > > model)? Calendar or Date?

    > >
    > > If you are NOT dealing with issues of displaying time in timezone other
    > > than that of the client, and you just need to display a time in the

    > timezone of
    > > the user then I would use the much more compact binary representation of

    > date
    > > and time, the java.util.Date.
    > >
    > > 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 would suggest you also look into SimpleDateFormat which takes
    > > a date and makes a String. This is usually what you'll need in
    > > a client application.
    > >
    > > -Paul

    >
    > The specific server routine returns a vector of objects (history). The
    > objects have a few dates as members, and these should be easily

    convertible
    > to strings (displaying), and easily to be used for comparison (post
    > selection operations - server side or client side) etc.


    For instance, in one routine I need the year from the date. I don't see how
    I can do that without the Calendar.

    Tom.

    > Since java.util.Date is 75% deprecated, and those include the members I

    need
    > to extract the values, I thought about using GregorianCalendar objects for
    > dates. Plus, no real conversion is necessary if the client (UI) needs a
    > gregoriancalendar. To be honest I haven't done too much UI-coding lately,
    > and I wouldn't know the best format to supply the dates.
    > I'm relatively new to java, and I am not used to finding so many date/time
    > related classes in a library.
    > Can you tell me how TimeStamp differs from Date for instance?
    >
    > Thanks,
    > Tom.
    >
    >
    TT \(Tom Tempelaere\), Jan 29, 2004
    #13
  14. TT \(Tom Tempelaere\)

    P.Hill Guest

    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
    P.Hill, Jan 29, 2004
    #14
  15. TT \(Tom Tempelaere\)

    P.Hill Guest

    TT (Tom Tempelaere) wrote:
    >>Can you find some method that allows you to send a TimeStamp into
    >>a calendar?

    >
    > TimeStamp derives from java.util.Date. I just wondered if it took
    > nanoseconds into account. Guess not.


    The API tells you that Timestamp (it doesn't have a capatilized
    S!) happens to be a subclass of Date, but that you should not
    just take a TimeStamp as is and treat it as a Date. Why because
    you will then lose the milliseconds.

    >>Can you find any discussion of nanoseconds in Calendar or DateFormat?
    >>Are you really interested in making a String out of a timestamp?
    >>Why? What are you going to do with it?

    >
    > My colleague is using that, I wouldn't know why either.


    There are uses for it. I asked for your uses not his.

    Do you still have any questions about using Date/Calendar/Timestamp?

    -Paul
    P.Hill, Jan 29, 2004
    #15
  16. "P.Hill" <> wrote in message
    news:bvc3q4$eqo$...
    > TT (Tom Tempelaere) wrote:

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


    Yes it does. Thank you.

    Tom.
    TT \(Tom Tempelaere\), Jan 30, 2004
    #16
  17. TT \(Tom Tempelaere\)

    Tony Morris Guest


    > So I should construct default GregorianCalendar, and then call setTime?


    No.
    Call Calendar.getInstance()

    This takes into account the fact that the VM is running in an environment
    where a Gregorian Calendar is used (i.e. earth).

    Move your software to another planet where the year is not 365.24 days long
    and the software falls over.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    Home : +61 7 5502 7987
    Work : +61 7 5552 4076
    Mobile : 0408 711 099
    (2003 VTR1000F)
    Tony Morris, Jan 31, 2004
    #17
  18. TT \(Tom Tempelaere\)

    P.Hill Guest

    Tony Morris wrote:
    >>So I should construct default GregorianCalendar, and then call setTime?

    >
    > No.
    > Call Calendar.getInstance()
    >
    > This takes into account the fact that the VM is running in an environment
    > where a Gregorian Calendar is used (i.e. earth).
    >
    > Move your software to another planet where the year is not 365.24 days long
    > and the software falls over.


    LOL,

    Actually not all of Earth uses the Gregorian. Other actually used
    calendars include Islamic, Persian, Indian, Jewish ...
    The idea is that you could end up with another calendar in
    another location including on Mars depending on locale settings.

    On the other hand, if you are trying to display a Gregorian
    date, or do calculations based on a Gregorian Calendar, then
    you should instantiate a Gregorian Calendar.

    For example, if you want to figure out when the next Christmas is
    then it's just a lot easier to start with a Gregorian
    then set( Calendar.MONTH, Calendar.DECEMBER )
    .... setCalendar( Calendar.DAY, 25 ) etc.

    It all depends.
    -Paul
    P.Hill, Feb 1, 2004
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manoj Nair
    Replies:
    4
    Views:
    21,336
    Manoj Nair
    Sep 4, 2003
  2. Erwin Moller
    Replies:
    80
    Views:
    8,635
    Erwin Moller
    Aug 27, 2004
  3. Replies:
    4
    Views:
    17,861
    John C. Bollinger
    Feb 8, 2005
  4. Replies:
    5
    Views:
    5,599
  5. KevinSimonson
    Replies:
    6
    Views:
    633
Loading...

Share This Page