f



Convert from java.sql.Date to GregorianCalendar

Hi,

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

Thanks,
Tom Tempelaere


0
1/29/2004 3:08:28 PM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

17 Replies
628 Views

Similar Articles

[PageSpeed] 54

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

0
goodhill (157)
1/29/2004 3:12:12 PM
"P.Hill" <goodhill@xmission.com> wrote in message
news:bvb7s9$1bc$2@terabinaries.xmission.com...
> 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.


0
1/29/2004 3:40:38 PM
"P.Hill" <goodhill@xmission.com> wrote in message
news:bvb7s9$1bc$2@terabinaries.xmission.com...
> 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.


0
1/29/2004 4:02:26 PM
"P.Hill" <goodhill@xmission.com> wrote in message
news:bvb7s9$1bc$2@terabinaries.xmission.com...
> 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.


0
1/29/2004 4:07:55 PM

TT (Tom Tempelaere) wrote:
> "P.Hill" <goodhill@xmission.com> 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

0
goodhill (157)
1/29/2004 5:06:24 PM
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

0
goodhill (157)
1/29/2004 5:12:24 PM

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

0
goodhill (157)
1/29/2004 5:16:29 PM
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

0
nobody89 (1641)
1/29/2004 5:42:04 PM
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

0
goodhill (157)
1/29/2004 6:01:37 PM
"P.Hill" <goodhill@xmission.com> wrote in message
news:bvbetm$47h$1@terabinaries.xmission.com...
> 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.


0
1/29/2004 8:27:24 PM
"P.Hill" <goodhill@xmission.com> wrote in message
news:bvbf5b$4bg$1@terabinaries.xmission.com...
>
>
> 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
>


0
1/29/2004 8:34:10 PM
"TT (Tom Tempelaere)" <_N_OSPAMtiti____@hotmail.comMAPSO_N_> wrote in
message news:M0eSb.12192$Vc7.341065@phobos.telenet-ops.be...
> "P.Hill" <goodhill@xmission.com> wrote in message
> news:bvbetm$47h$1@terabinaries.xmission.com...
> > 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.
>
>


0
1/29/2004 9:28:02 PM
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

0
goodhill (157)
1/29/2004 11:08:54 PM
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

0
goodhill (157)
1/29/2004 11:12:52 PM
"P.Hill" <goodhill@xmission.com> wrote in message
news:bvc3q4$eqo$1@terabinaries.xmission.com...
> 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.


0
1/30/2004 8:32:48 AM
> 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)


0
dibblego (448)
1/31/2004 8:13:06 AM
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

0
goodhill (157)
2/1/2004 1:04:56 AM
Reply: