While using both classes I found that for creating an object for respective classes Like
GregorianCalendar today = new GregorianCalendar ();
Calendar today = Calendar.getInstance();
Why it is so? And what is benefits of using one over another?
In most locales, when you write Calendar.getInstance()
, it will create a GregorianCalendar
object. But there are a few exceptions. Your locale could be set to give you a JapaneseImperialCalendar
or a BuddhistCalendar
.
If you really need your Calendar
to be a GregorianCalendar
, then you can explicitly write new GregorianCalendar()
. But it's generally considered better form to let the JVM choose the right kind of Calendar
based on the locale. That is, you should write Calendar.getInstance()
, and be prepared to deal with the possibility of using one of the other calendar types.
There is no logic in the Calendar
class for getInstance()
to return one of the other types of calendar, such as those used in Iran or Saudi Arabia.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With