Calendar question

R

Rhino

Can someone clarify for me the difference between:

GregorianCalendar now = new GregorianCalendar();

and

Calendar now = Calendar.getInstance();

and

Calendar now = GregorianCalendar.getInstance();

I'm really not clear on what each does and when I would prefer one over the
other. (I assume each one is preferred in some situation or another.)
 
L

Lew

Rhino said:
Can someone clarify for me the difference between:

  GregorianCalendar now = new GregorianCalendar();

<http://java.sun.com/javase/6/docs/api/java/util/
GregorianCalendar.html#GregorianCalendar()>
"Constructs a default GregorianCalendar using the current time in the
default time zone with the default locale."
and

  Calendar now = Calendar.getInstance();

<http://java.sun.com/javase/6/docs/api/java/util/
Calendar.html#getInstance()>
"Gets a calendar using the default time zone and locale. ... based on
the current time ..."
Note that it does not say it gets a 'GregorianCalendar' instance.
and

  Calendar now = GregorianCalendar.getInstance();

'GregorianCalendar' does not have a 'getInstance()' method. You
should refer to static members by the type that declares them, not
through subtypes.
I'm really not clear on what each does and when I would prefer one over the
other. (I assume each one is preferred in some situation or another.)

Use 'new GregorianCalendar()' when you specifically want to construct
a 'GregorianCalendar' and not some other 'Calendar' type. You might
want this if the default 'Calendar' for the platform is some other
type, or if you want to control aspects not provided by the 'Calendar'
type, e.g., leap years.

For all other purposes, especially if you particularly want the
'Calendar' native to the host platform, use 'Calendar.getInstance()'.
In practice, this is nearly all the time.

This information is a combination of what's in the Javadocs, which of
course you have read thoroughly, and normal Java best practices, which
of course you are always studying.

<http://java.sun.com/docs/books/effective/>
"Item 1: Consider static factory methods instead of constructors"

which, of course, you have read and continue to reread periodically.
 
J

Jean-Baptiste Nizet

Can someone clarify for me the difference between:

  GregorianCalendar now = new GregorianCalendar();

This one creates a new instance of GregorianCalendar.
and

  Calendar now = Calendar.getInstance();

This one creates a Calendar instance. The concrete type of the created
Calendar instance depends on the default timezone and locale. If you
look at the source code, you'll discover that it usually creates a
GregorianCalendar instance, but can also create a BuddhistCalendar or
JapaneseImperialCalendar instance.
and

  Calendar now = GregorianCalendar.getInstance();

This one actually calls Calendar.getInstance(), since there is no
getInstance method in GregorianCalendar. It's thus exactly the same as
the previous one, except I would consider it bad style.
I'm really not clear on what each does and when I would prefer one over the
other.

If you want a Calendar instance that is the most appropriate for the
country and locale of the system where your app is executing, use
Calendar.getInstance(). If you *need* a GregorianCalendar, regardless
of the default locale and timezone, use new GregorianCalendar().

JB.
 
R

Rhino

<http://java.sun.com/javase/6/docs/api/java/util/
GregorianCalendar.html#GregorianCalendar()>
"Constructs a default GregorianCalendar using the current time in the
default time zone with the default locale."


<http://java.sun.com/javase/6/docs/api/java/util/
Calendar.html#getInstance()>
"Gets a calendar using the default time zone and locale. ... based on
the current time ..."
Note that it does not say it gets a 'GregorianCalendar' instance.
I'm quite aware of the Javadocs for the API and did consult them before I
asked my question. I just didn't understand what I was seeing and was
hoping for a clarification.
'GregorianCalendar' does not have a 'getInstance()' method. You
should refer to static members by the type that declares them, not
through subtypes.
Sorry, my mistake. I had misremembered a line I saw in the API but that
line was actually Calendar rightNow = Calendar.getInstance().
Use 'new GregorianCalendar()' when you specifically want to construct
a 'GregorianCalendar' and not some other 'Calendar' type. You might
want this if the default 'Calendar' for the platform is some other
type, or if you want to control aspects not provided by the 'Calendar'
type, e.g., leap years.

For all other purposes, especially if you particularly want the
'Calendar' native to the host platform, use 'Calendar.getInstance()'.
In practice, this is nearly all the time.

This information is a combination of what's in the Javadocs, which of
course you have read thoroughly, and normal Java best practices, which
of course you are always studying.
Sarcasm noted.

Again, I DID read the API but didn't entirely follow what it was saying.
I DO care about Java best practices, which is why I'm asking here,
looking for advice from "Java gurus".
<http://java.sun.com/docs/books/effective/>
"Item 1: Consider static factory methods instead of constructors"

which, of course, you have read and continue to reread periodically.
Further sarcasm noted.

And no, I do not have this book and therefore do not consult it. I
probably SHOULD have it but money is tight and I'm trying to make do
without it as best I can.
 
R

Rhino

This one creates a new instance of GregorianCalendar.


This one creates a Calendar instance. The concrete type of the created
Calendar instance depends on the default timezone and locale. If you
look at the source code, you'll discover that it usually creates a
GregorianCalendar instance, but can also create a BuddhistCalendar or
JapaneseImperialCalendar instance.


This one actually calls Calendar.getInstance(), since there is no
getInstance method in GregorianCalendar. It's thus exactly the same as
the previous one, except I would consider it bad style.
My mistake; I misremembered a line I had seen in the Calendar API which
said Calendar rightnow = Calendar.getInstance().
If you want a Calendar instance that is the most appropriate for the
country and locale of the system where your app is executing, use
Calendar.getInstance(). If you *need* a GregorianCalendar, regardless
of the default locale and timezone, use new GregorianCalendar().

JB.

Thank you, that is helpful!
 
L

Lew

Sarcasm noted.

Why do you think that's sarcasm? It is clear from your posts that my
comment is literally true, and I am not so stupid as not to have
noticed that.

Oh, wait, I forgot to turn on the "THIS IS HUMOR" sign again. I keep
forgetting how thin-skinned and humorless people are around here.
Yeesh!
Again, I DID read the API but didn't entirely follow what it was saying.
I DO care about Java best practices, which is why I'm asking here,
looking for advice from "Java gurus".

Yes, yes, yes. Of course.

As I said, that's already obvious from your posts. Grab a beer and
chill out.

Lew strongly recommends:
Further sarcasm noted.

It's not sarcasm, it's advice couched in terms of humor.

Remember humor? It's that thing people do to spice up a conversation
and make it a little less dry.

Maybe you need two beers.
 
L

Lew

My mistake; I misremembered a line I had seen in the Calendar API which
said Calendar rightnow = Calendar.getInstance().






Thank you, that is helpful!

And eerily reminiscent of:
 
R

Rhino

Why do you think that's sarcasm? It is clear from your posts that my
comment is literally true, and I am not so stupid as not to have
noticed that.
As I have assured you, I DID read the API.
Oh, wait, I forgot to turn on the "THIS IS HUMOR" sign again. I keep
forgetting how thin-skinned and humorless people are around here.
Yeesh!
If that was humour, it sure didn't come across that way. In fact it came
across as patronizing in the extreme.

Most Usenet patrons use emoticons/smilies to denote humour and I didn't
see any in your posts. I therefore assumed that it was delivered in a
serious and scornful tone.
Yes, yes, yes. Of course.

As I said, that's already obvious from your posts. Grab a beer and
chill out.

Lew strongly recommends:


It's not sarcasm, it's advice couched in terms of humor.

Remember humor? It's that thing people do to spice up a conversation
and make it a little less dry.

Maybe you need two beers.
Maybe you need to start using emoticons so the rest of us can determine
when you're indulging in humour. I've been away from this newsgroup for
several years and I don't remember you from my previous visits so I don't
know when you're kidding in the absence of emoticons.
 
L

Lew

Rhino wrote:
As I have assured you, I DID read the API.

As I have assured you, I DID realize that.
If that was humour, it sure didn't come across that way. In fact it came
across as patronizing in the extreme.


Boo-hoo-hoo.

:) ;-) :-o :-}
Most Usenet patrons use emoticons/smilies to denote humour and I didn't
see any in your posts. I therefore assumed that it was delivered in a
serious and scornful tone.
Awwwww.

;-)
Maybe you need to start using emoticons so the rest of us can determine
when you're indulging in humour. I've been away from this newsgroup for
several years and I don't remember you from my previous visits so I don't
know when you're kidding in the absence of emoticons.

Holy moly! You sure are jumpy. Freaking relax, already!

;-) :-} :-/ :-\ 3:-0

There's a little thing called "context" that you might try gleaning from. K?

And make that three beers. You are wound tighter than a Tesla coil.
 
R

Rhino

Lew said:
Rhino wrote:



As I have assured you, I DID realize that.




Boo-hoo-hoo.

:) ;-) :-o :-}
And there's a perfect example of the difference an emoticon makes:
without the emoticon, your remark looks condescending, even bullying.
With the emoticon, it might be taken for a joke.

Of course, with several emoticons, it's overkill and starts to look
condescending again....
Holy moly! You sure are jumpy. Freaking relax, already!
I'm trying to explain why it wasn't obvious to me that you were joking
around. Now you try to make me out to be a sorehead or humourless knob
when a gracious person (and I'm thinking of you when I say that) might
just learn from their mistake and move on.
;-) :-} :-/ :-\ 3:-0
Using obscure emoticons that I'll have to look up - meaning everything
after the second one - doesn't actually facilitate communication. It
would be like me replying in French and assuming you would eagerly run
off to a French-English dictionary to see what 'bon mots' I had written.
There's a little thing called "context" that you might try gleaning
from. K?
And now you're being patronizing again by suggesting that I'm unaware of
context.
And make that three beers. You are wound tighter than a Tesla coil.
Again you're putting 100% of the blame on me for misunderstanding rather
than accepting that I might reasonably not know that you were kidding
around. In my experience, misunderstandings are usually the fault of BOTH
people who are communicating.
 
J

Johannes Schneider

Rhino said:
Can someone clarify for me the difference between:

GregorianCalendar now = new GregorianCalendar();

and

Calendar now = Calendar.getInstance();

and

Calendar now = GregorianCalendar.getInstance();

There is a simple answer for that one. Use Joda-Time...
There should be a very good reason to use that Calendar stuff...


Sincerely,

Johannes
 
A

Arne Vajhøj

There is a simple answer for that one. Use Joda-Time...
There should be a very good reason to use that Calendar stuff...

Using standard Java functionality is a pretty good
reason.

Arne
 

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,851
Messages
2,569,772
Members
45,555
Latest member
isabelferr

Latest Threads

Top