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?
views:
68answers:
2
+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
2010-08-08 19:36:00
+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
2010-08-08 19:39:09
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
2010-08-08 19:42:12
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
2010-08-08 21:00:11