The DateBean class is pretty much as written below.  You would have to add a
getXXXDateBean method for every date property that you want to wrap.

If you don't mind using deprecated APIs, you could also create a custom
org.apache.commons.betwixt.strategy.TypeBindingStrategy that returns
COMPLEX_TYPE for Date.class and then create a java/util/Date.betwixt mapping
file with the properties "day", "month", and "year" (java.util.Date has
getDay(), getMonth(), and getYear(), which are deprecated).  That would save
you from having to create a separate bean and add all the extra getters and
setters, but it would apply to *all* dates, which may not be desirable.


-----Original Message-----
From: Peter Konstantinov [mailto:[EMAIL PROTECTED] 
Sent: Monday, October 09, 2006 12:52 PM
To: Jakarta Commons Users List
Cc: Peter Konstantinov
Subject: Re: [betwixt] Bean to Bean mapping

Can you help to write DateBean.betwixt?

Is it possible to do this without writing getMyDateBean method in all  
classes using Date?

On 09.10.2006, at 21:21, Sweetland, Jesse wrote:

> Betwixt only converts value-for-value.  If you need to break the  
> date out
> like that then you need to create a new element bean:
>
> public class MyClass {
>       private Date _myDate = new Date();
>
>       public Date getMyDate() {
>               return _myDate;
>       }
>
>       public DateBean getMyDateBean() {
>               return new DateBean(_myDate);
>       }
> }
>
> public class DateBean {
>       private Calendar _calendar = Calendar.getInstance();
>       
>       public int getDay() {
>               return _calendar.get(Calendar.DATE);
>       }
>
>       public int getMonth() {
>               return _calendar.get(Calendar.MONTH + 1);
>       }
>
>       public int getYear() {
>               return _calendar.get(Calendar.YEAR);
>       }
>
>       public DateBean(Date date) {
>               _calendar.setTime(date);
>       }
> }
>
> Then map that bean using a DateBean.betwixt file.  You can also add  
> setters
> for round-tripping.
>
> Hope that helps,
>
> Jesse
>
> -----Original Message-----
> From: Peter Konstantinov [mailto:[EMAIL PROTECTED]
> Sent: Monday, October 09, 2006 12:11 PM
> To: commons-user@jakarta.apache.org
> Subject: [betwixt] Bean to Bean mapping
>
> Hi,
> How to convert java.util.Date to same xml:
> <date day="..." month="..." year="..." />
> ConvertUtilsObjectStringConverter can convert only to String.
>
> Regards,
> Peter
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to