LuxuryMode
LuxuryMode

Reputation: 33741

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.

Upvotes: 26

Views: 50718

Answers (2)

aroth
aroth

Reputation: 54816

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.

Upvotes: 51

Kyle
Kyle

Reputation: 4298

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

Upvotes: 13

Related Questions