开发者

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

开发者 https://www.devze.com 2023-01-11 04:08 出处:网络
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 ho

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

精彩评论

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