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.
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.
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.
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.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With