This discussion is archived
2 Replies Latest reply: Aug 7, 2007 2:09 PM by 796440 RSS

Calendar properties

807605 Newbie
Currently Being Moderated
Salutations.

I have been having a great deal of frustration trying to get the MONTH, YEAR, and DATE out of the Calendar.

I used Calendar.InstanceOf().toString(), and the value was as follows:

"java.util.GregorianCalendar[time=1186520352230,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=\"America/New_York\",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2007,MONTH=7,WEEK_OF_YEAR=32,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=219,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=59,SECOND=12,MILLISECOND=230,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]"

As you can see, the year is 2007, the month is 7, and the DAY_OF_MONTH is 7.

However, when I use the Calendar.MONTH or
Calendar.InstanceOf().Month I get: 2

For YEAR: 1

For Day: 5.


Any ideas?
  • 1. Re: Calendar properties
    807605 Newbie
    Currently Being Moderated
    Yeh. Those are constants, they don't represent properties of a Calendar, they're used to access the properties of a Calendar. Try
    Calendar cal = Calendar.getInstance();
    int month = cal.get(Calendar.MONTH);
    The constant MONTH belongs to the class, Calendar, you use it to tell the instance of Calendar which property you're interested in
  • 2. Re: Calendar properties
    796440 Guru
    Currently Being Moderated
    the month is 7, and
    Not that it's relevant to your original question, but the numerical value of the month in a Calendar is not relevant. You don't care what that value is. Don't try to use those values directly.


    However, when I use the Calendar.MONTH or
    Calendar.InstanceOf().Month I get: 2
    Calendar.MONTH isn't the value of that Calendar's month. It's simply the constant that indicates that the month is the field you're interested in.