简体   繁体   中英

Calendar.getInstance()… basic question

Does Calendar.getInstance( ) work like a regular singleton in the sense that if I've called getInstance somewhere else and set the day, month, etc then if I call Calendar.getInstance() somewhere else those fields will be set to whatever I set them before? In other words, if I want the Calendar.getInstance() to return a Calendar object with the current time and day, etc, what do I need to do? Just call clear() ? Does that reset the instance to the current time, etc?

My apologies if this is a stupid question.

No, Calendar.getInstance() is not returning a singleton instance. The point of using getInstance() is that it will take the default locale and time-zone into account when deciding which Calendar implementation to return and how to initialize it.

So you don't need to do anything to get a second Calendar with the current time, just call Calendar.getInstance() again.

Generally you can use the API documentation (see source) for questions like this. From the API documentation:

Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time:

  Calendar rightNow = Calendar.getInstance(); 

Source: http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM