Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calendar.getInstance()... basic question

Tags:

java

calendar

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.

like image 528
LuxuryMode Avatar asked Aug 07 '11 23:08

LuxuryMode


People also ask

What is the use of calendar getInstance () in Java?

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

What is calendar getInstance?

The getInstance() method in Calendar class is used to get a calendar using the current time zone and locale of the system. Syntax: public static Calendar getInstance() Parameters: The method does not take any parameters. Return Value: The method returns the calendar.

What does calendar getInstance () getTime () this return?

Calendar. getInstance(). getTime() : Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch(January 1, 1970 00:00:00.000 GMT (Gregorian).)


1 Answers

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.

like image 57
aroth Avatar answered Oct 15 '22 08:10

aroth