wood burning stoves*
The moose likes Java in General and the fly likes How to get Calendar property value from a Java class using reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get Calendar property value from a Java class using reflection" Watch "How to get Calendar property value from a Java class using reflection" New topic
Author

How to get Calendar property value from a Java class using reflection

Pawan Kalyan
Ranch Hand

Joined: Jun 18, 2009
Posts: 34
Hi,

I am unable to get the value of a property inside a bean which is of type Calendar in Calendar format.

I am currently using BeanUtils (org.apache.commons.beanutils.PropertyUtils) to get the property value but it returns the value as string, which i am unable to type cast to Calendar.



If i print out the value then it looks something like,

java.util.GregorianCalendar[time=1341028800000,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=2012,MONTH=5,WEEK_OF_YEAR=26,WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=182,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]

Is there a way i can get the value in Calendar format or convert the above string into calendar format ?

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3504
    
  14
Pawan Kalyan wrote:I am currently using BeanUtils (org.apache.commons.beanutils.PropertyUtils) to get the property value but it returns the value as string, which i am unable to type cast to Calendar.

If i print out the value then it looks something like,

java.util.GregorianCalendar[time=1341028800000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,...

Is there a way i can get the value in Calendar format or convert the above string into calendar format ?



You've got it in Calendar format (a GregorianCalendar instance to be precise). The output you show is what calendar.toString returns. Your problem is probably in how you try to cast it to a Calendar. Show us the code where you do that.

Joanne
Pawan Kalyan
Ranch Hand

Joined: Jun 18, 2009
Posts: 34
I am trying to do



Then i get the following error



java.lang.String incompatible with java.util.Calendar


And when i looked into BeanUtils API, it says that using BeanUtils.getProperty or BeanUtils.getSimpleProperty() will return the value in String format thats why when i type cast the value to Calendar it throws a run time error.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3504
    
  14
So how did you print out the value you showed in your first post ?
Pawan Kalyan
Ranch Hand

Joined: Jun 18, 2009
Posts: 34
using System.out.println(value) to see what was in that value.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4458
    
    6

Any reason you're not just calling bean.getDateOfBirth() directly?


Junilu - [How to Ask Questions] [How to Answer Questions]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3504
    
  14
Pawan Kalyan wrote:using System.out.println(value) to see what was in that value.

Okay. I see what your saying. The getProperty method calls toString on the object and then returns that String.
I'm afraid I don't know anything about the BeanUtils class, so i would say you need to either follow Junilu's advice and call bean.getDateOfBirth() directly, find another method in the BeanUtil class that returns the object rather than its String representation or (and this is definitely not recommended) parse the returned String to get the information you want.
Pawan Kalyan
Ranch Hand

Joined: Jun 18, 2009
Posts: 34
I cant do bean.getDateOfBirth() becasue i am using this code in a common method to get values from bean and to set those values on other bean which have same variable names and types using reflection. thats why i was trying to see if there is any other way to get a property value of a java bean in Object or Calendar value if the property type is Calendar.

I hope i didnt confuse you and understand my condition.

Please Let me know if there is any other way.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4458
    
    6

If you make the types and property names on the source and target beans the same, the BeanUtils static methods copyProperty() or copyProperties() should do the trick. If the types are not the same on both, then you should rethink your design.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get Calendar property value from a Java class using reflection