开发者

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 neit开发者_JAVA技巧her from the internet. This may sound like a newbie question but how does this method work?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜