• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how did you print out the value you showed in your first post ?
 
Pawan Kalyan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using System.out.println(value) to see what was in that value.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any reason you're not just calling bean.getDateOfBirth() directly?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic