Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Convert from java.sql.Date to GregorianCalendar

Reply
Thread Tools

Convert from java.sql.Date to GregorianCalendar

 
 
TT \(Tom Tempelaere\)
Guest
Posts: n/a
 
      01-29-2004
Hi,

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

Thanks,
Tom Tempelaere


 
Reply With Quote
 
 
 
 
P.Hill
Guest
Posts: n/a
 
      01-29-2004
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

 
Reply With Quote
 
 
 
 
TT \(Tom Tempelaere\)
Guest
Posts: n/a
 
      01-29-2004
"P.Hill" <(E-Mail Removed)> wrote in message
news:bvb7s9$1bc$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
TT \(Tom Tempelaere\)
Guest
Posts: n/a
 
      01-29-2004
"P.Hill" <(E-Mail Removed)> wrote in message
news:bvb7s9$1bc$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
TT \(Tom Tempelaere\)
Guest
Posts: n/a
 
      01-29-2004
"P.Hill" <(E-Mail Removed)> wrote in message
news:bvb7s9$1bc$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      01-29-2004


TT (Tom Tempelaere) wrote:
> "P.Hill" <(E-Mail Removed)> 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

 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      01-29-2004
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

 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      01-29-2004


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

 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      01-29-2004
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

 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      01-29-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert SimpleDateFormat to GregorianCalendar yelledbett@gmail.com Java 4 02-08-2005 07:26 PM
GregorianCalendar - it's a bomb ;-) [code insight] Xandau Java 9 06-25-2004 12:45 PM
GregorianCalendar Andreas N Rasmussen Java 5 12-06-2003 12:31 AM
GregorianCalendar applet and StreamCorruptedException Nigel Wade Java 10 11-24-2003 05:15 PM
GregorianCalendar and how to subtract MONTH from a date. Manoj Nair Java 4 09-04-2003 05:49 AM



Advertisments