Saher Ahwal
Saher Ahwal

Reputation: 9237

How does Calendar.getInstance() know the current year?

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?

Upvotes: 2

Views: 1109

Answers (2)

jqno
jqno

Reputation: 15520

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().

Upvotes: 8

whiskeysierra
whiskeysierra

Reputation: 5120

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.

Upvotes: 2

Related Questions