Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between GregorianCalendar class and Calendar class in java?

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?

like image 884
0xC0d3 Avatar asked Dec 04 '22 21:12

0xC0d3


1 Answers

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.

like image 192
Dawood ibn Kareem Avatar answered Dec 11 '22 17:12

Dawood ibn Kareem