From: Rhino on 25 Mar 2010 09:44 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.) -- Rhino
From: Lew on 25 Mar 2010 10:05 Rhino wrote: > 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. -- Lew
From: Jean-Baptiste Nizet on 25 Mar 2010 10:12 On 25 mar, 14:44, Rhino <no.offline.contact.ple...(a)example.com> wrote: > 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. > (I assume each one is preferred in some situation or another.) > > -- > Rhino
From: Rhino on 25 Mar 2010 14:10 Lew <lew(a)lewscanon.com> wrote in news:7ef177c8-7694-4aa4-888b-44907d699b10(a)b33g2000yqc.googlegroups.com: > Rhino wrote: >> 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. > 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. >> 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. > Sorry, my mistake. I had misremembered a line I saw in the API but that line was actually Calendar rightNow = Calendar.getInstance(). >> I'm really not clear on what each does and when I would prefer one >> over t > he >> 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. > 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. -- Rhino
From: Rhino on 25 Mar 2010 14:12
Jean-Baptiste Nizet <jnizet(a)gmail.com> wrote in news:27538630-6525-438e-ad4b-2ad35a5fecc5(a)v20g2000yqv.googlegroups.com: > On 25 mar, 14:44, Rhino <no.offline.contact.ple...(a)example.com> wrote: >> 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. > My mistake; I misremembered a line I had seen in the Calendar API which said Calendar rightnow = Calendar.getInstance(). >> I'm really not clear on what each does and when I would prefer one >> over t > he >> 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. > >> (I assume each one is preferred in some situation or another.) >> >> -- >> Rhino > Thank you, that is helpful! -- Rhino |