The implement of
  org.apache.commons.beanutils.ConvertUtilsBean#convert(java.lang.Object)
is:

>> final Converter converter = lookup(String.class);
>> return (converter.convert(String.class, value));

This is why BeanUtils.describe(...) method calls StringConverter to convert
Date value to String.
I think it is not right to lookup by String class.
It may be better if lookup by the class of the value as the sourceType, and
String as the targetType:

>> final Converter converter = lookup(value.getClass(), String.class);
>> return (converter.convert(String.class, value));


And then, we can use DateConverter for Data class, and
SqlTimestampConverter for sql Timestamp:

>> String pattern = "yyyy-MM-dd HH:mm:ss.SSS";
>>
>> ConvertUtilsBean convertUtils =
BeanUtilsBean.getInstance().getConvertUtils();
>>
>> DateConverter dateConverter = new DateConverter();
>> dateConverter.setPattern(pattern);
>> convertUtils.register(dateConverter, Date.class);
>>
>> SqlTimestampConverter timestampConverter = new SqlTimestampConverter();
>> timestampConverter.setPattern(pattern);
>> convertUtils.register(timestampConverter, Timestamp.class);



=========================================
李 穎 (リ エイ / Li Ying)
EMail: [email protected]
Mobile: (+81)-080-5403-9083
=========================================



2017-03-28 12:56 GMT+09:00 Li Ying <[email protected]>:

> Hi Harri:
>
>
> I was wrong for this:
> >> Looks like it calls DateTimeConverter#convertToString method to
> convert Data value to String.
>
> Actually, BeanUtils.describe(...) method calls StringConverter.
> And this StringConverter class uses the implement in the parent class
> AbstractConverter#convertToString:
> >> protected String convertToString(final Object value) throws Throwable {
> >>     return value.toString();
> >> }
>
>
> I write some test code:
>
> >>  TestBean testBean = new TestBean();
> >>
> >>  Date now = new Date();
> >>  Timestamp sqlTimestamp = new Timestamp(now.getTime());
> >>
> >>  testBean.setDate01(sqlTimestamp);
> >>  testBean.setDate02(now);
> >>
> >>  Map<String, String> describe = BeanUtils.describe(testBean);
> >>
> >>  System.out.println("describe = " + describe);
>
> The output is:
> >>    describe = {date02=Tue Mar 28 12:28:14 JST 2017, date01=2017-03-28
> 12:28:14.355}
>
> The solution is, overriding the Converter setting by adding the following
> code:
>
> >> ConvertUtilsBean convertUtils = BeanUtilsBean.getInstance().
> getConvertUtils();
> >>
> >> DateConverter dateConverter = new DateConverter();
> >> dateConverter.setPattern("yyyy-MM-dd HH:mm:ss.SSS");
> >> convertUtils.register(dateConverter, String.class);
>
> And now the output is:
> >> describe = {date02=2017-03-28 12:46:43.560, date01=2017-03-28
> 12:46:43.560}
>
> But to register a DateConverter to the String class is not good.
> >> convertUtils.register(dateConverter, String.class);
>
> Maybe we should write a sub-class of StringConverter,
> and fix the format problem of the date value in this sub-class.
>
>
>
>
>
> =========================================
> 李 穎 (リ エイ / Li Ying)
> EMail: [email protected]
> Mobile: (+81)-080-5403-9083 <+81%2080-5403-9083>
> =========================================
>
> 2017-03-28 10:41 GMT+09:00 Li Ying <[email protected]>:
>
>> Hi Harri:
>>
>> I read a little source code of the BeanUtils.describe(...) method.
>> Looks like it calls DateTimeConverter#convertToString method to convert
>> Data value to String.
>> And in the following source code:
>>
>> >> if (useLocaleFormat && date != null) {
>> >>     .....
>> >> } else {
>> >>     result = value.toString();
>> >> }
>>
>> If there is no format to use, it will simply call value.toString().
>>
>> So I think maybe your [multiple java.util.Date properties] are actually
>> different sub-class of Date,
>> and their toString() methods are using different format.
>>
>>
>>
>> =========================================
>> 李 穎 (リ エイ / Li Ying)
>> EMail: [email protected]
>> Mobile: (+81)-080-5403-9083 <+81%2080-5403-9083>
>> =========================================
>>
>>
>>
>> 2017-03-28 2:09 GMT+09:00 Harri T. <[email protected]>:
>>
>>> Hi,
>>>
>>> I have a bean with multiple java.util.Date properties. Why does
>>> BeanUtils.describe(...) return some of them in format
>>>
>>>   2017-03-27 19:50:21.531
>>>
>>> and others in format
>>>
>>>   Mon Mar 27 19:50:28 EEST 2017
>>>
>>> ?
>>>
>>> The first format is preferred.
>>>
>>> Harri
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [email protected]
>>> For additional commands, e-mail: [email protected]
>>>
>>>
>>
>

Reply via email to