tags:

views:

68

answers:

2

How does Calendar.getInstance() method get you the current year? It doesn't read it from your computer obviously neither from the internet. This may sound like a newbie question but how does this method work?

+5  A: 

Actually, it does read it from your computer. Internally, it calls GregorianCalendar's constructor, which calls System.currentTimeMillis(), which is a native method.

Depending on your locale, it might also create a JapaneseImperialCalendar or a BuddhistCalendar, which also both call System.currentTimeMillis().

jqno
+2  A: 
Calendar.getInstance()

is just a shortcut for

new GregorianCalendar()

which initializes itself using:

setTimeInMillis(System.currentTimeMillis());

So the trick is

System.currentTimeMillis()

which indeed does read it from your computer.

Willi
It isn't a shortcut. It returns the suitable instance for the current locale. In locales using a Gregorian calendar, a `GregorianCalendar` will be returned by the abstract factory method.
BalusC
You are right. I was looking here (http://www.docjar.com/html/api/java/util/Calendar.java.html, line 984) which seems to differ from the JDK.
Willi