Convert from java.sql.Date to GregorianCalendar

  • Thread starter TT \(Tom Tempelaere\)
  • Start date
T

TT \(Tom Tempelaere\)

Hi,

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

Thanks,
Tom Tempelaere
 
T

TT \(Tom Tempelaere\)

P.Hill said:
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.
 
T

TT \(Tom Tempelaere\)

P.Hill said:
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.
 
T

TT \(Tom Tempelaere\)

P.Hill said:
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.
 
P

P.Hill

TT said:
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

P.Hill

TT said:
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

P.Hill

TT said:
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
 
T

Thomas Weidenfeller

TT said:
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
 
P

P.Hill

Thomas said:
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
 
T

TT \(Tom Tempelaere\)

P.Hill said:
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.
 
T

TT \(Tom Tempelaere\)

P.Hill said:
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.
 
T

TT \(Tom Tempelaere\)

TT (Tom Tempelaere) said:
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.
 
P

P.Hill

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.

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

P.Hill

TT said:
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.
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
 
T

TT \(Tom Tempelaere\)

P.Hill said:
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.
 
T

Tony Morris

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)
 
P

P.Hill

Tony said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top