Hi, whilst reading a book on Java, I came across a bit where the author used

GregorianCalendar now = new GregorainCalendar();

And I was just wondering, is that any different from

Calendar now = Calendar.getInstance();

And if so, why did he use GregorianCalendar instead of the normal one?

Dani AI

Generated

Short answer: in typical Oracle/OpenJDK runtimes there is no visible difference for "current date" usage — the factory produces a Calendar implementation that is usually GregorianCalendar — but they are not identical concepts. Calendar.getInstance() is a locale/time-zone aware factory; GregorianCalendar is the concrete class and exposes extra behavior you will not get from the abstract Calendar type. See the Calendar docs for the factory behavior and the GregorianCalendar class for its extra API.

Use cases and practical differences:

  • If you only need a date/time now and you program to the abstract API, Calendar.getInstance() is the simpler, more flexible choice (it can return a different concrete implementation for some locales/time-zones). Calendar docs
  • If you need Gregorian-specific features (leap-year rules, the historical cutover between Julian/Gregorian, or methods like isLeapYear / getGregorianChange), instantiate GregorianCalendar directly. Those points were already noted by and and are worth keeping in mind. GregorianCalendar docs

Caveats and modern guidance:

  • Calendar/GregorianCalendar are mutable and error-prone (months are zero-based, time-zone and DST effects can bite).
  • For new code prefer the java.time API (JSR-310) introduced in Java 8: immutable, clearer types (LocalDate, ZonedDateTime, Instant). java.time package

Interop example (Java 8+):

ZonedDateTime zdt = ZonedDateTime.now();
GregorianCalendar gc = GregorianCalendar.from(zdt);
ZonedDateTime back = gc.toZonedDateTime();

Recommendation: keep using Calendar.getInstance() for general use; use GregorianCalendar only when you need its specific behavior; and migrate new code to java.time.

Recommended Answers

All 3 Replies

try this web link and read the comments at top:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html

essentially as it says:

This implementation handles a single discontinuity, which corresponds by default to the date the Gregorian calendar was instituted (October 15, 1582 in some countries, later in others). The cutover date may be changed by the caller by calling setGregorianChange().

// end quote

I think this would be useful if you have an application that uses historical time. One method is i'm going to feed you dates based on how we understand the calander works now with that date of change over, the gregorian seems to say if we are in a period of history were they may or may not have used gregorian time i can handle it. The calander before gregorian ( what we use now ) was julian which can be off a few days as you go back centuries do to slightly different leap year rules. i think they ommit skipping a leap year every 400 years in julian.

Mike

GregorianCalendar(concrete class) is a subclass of Calendar(abstract class).So u can use either....So u can say GregorianCaendar c=new GregorianCalendar.......thanks

Thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.