Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can i have only one instance of Calendar object

Tags:

I was just wondering...

why can i have only one instance of Calendar object. Is there a reason for it to be a singleton?

I have tried to read the documentation but they didn't mention why this is needed. And a quick google search didn't give me any answers.

like image 757
Gabriel Avatar asked May 19 '11 07:05

Gabriel


People also ask

How do I get a calendar instance?

Java Calendar getInstance() Method The getInstance() method of java. util. Calendar class is a Static method. This method is used with calendar object to get the instance of calendar according to current time zone set by java Runtime environment.

Is Java calendar a singleton?

Calendar is not a singleton, it is an abstract class. The getInstance method is a Factory method that returns a concrete implementation of the Calendar class. Search Google for java.


2 Answers

Calendar is not a singleton, it is an abstract class. The getInstance method is a Factory method that returns a concrete implementation of the Calendar class.

Search Google for java.util.Calendar source code, and you will see how it works.

like image 94
Codemwnci Avatar answered Oct 06 '22 16:10

Codemwnci


It is not singleton.

This:

public static void main(String args[]) {         Calendar c1, c2;         c1 = Calendar.getInstance();         c2 = Calendar.getInstance();         c1.add(Calendar.MONTH, 1);         System.out.println(c1);         System.out.println(c2);     } 

Outputs:

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Jerusalem",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Asia/Jerusalem,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=26,startDayOfWeek=6,startTime=7200000,startTimeMode=0,endMode=1,endMonth=8,endDay=13,endDayOfWeek=0,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=5,WEEK_OF_YEAR=21,WEEK_OF_MONTH=3,DAY_OF_MONTH=19,DAY_OF_YEAR=139,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=21,SECOND=27,MILLISECOND=839,ZONE_OFFSET=7200000,DST_OFFSET=3600000] java.util.GregorianCalendar[time=1305789687839,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Jerusalem",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Asia/Jerusalem,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=26,startDayOfWeek=6,startTime=7200000,startTimeMode=0,endMode=1,endMonth=8,endDay=13,endDayOfWeek=0,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=3,DAY_OF_MONTH=19,DAY_OF_YEAR=139,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=21,SECOND=27,MILLISECOND=839,ZONE_OFFSET=7200000,DST_OFFSET=3600000] 

(Which is different as you can see)

BTW, quick search for source code returns:

public static synchronized Calendar getInstance() {        return new GregorianCalendar(); } 
like image 22
MByD Avatar answered Oct 06 '22 16:10

MByD