commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Li Ying <liying.cn.2...@gmail.com>
Subject Re: BeanUtils.describe(...) Date Format
Date Tue, 28 Mar 2017 03:56:32 GMT
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: liying.cn.2010@gmail.com
Mobile: (+81)-080-5403-9083
=========================================

2017-03-28 10:41 GMT+09:00 Li Ying <liying.cn.2010@gmail.com>:

> 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: liying.cn.2010@gmail.com
> Mobile: (+81)-080-5403-9083 <+81%2080-5403-9083>
> =========================================
>
>
>
> 2017-03-28 2:09 GMT+09:00 Harri T. <haba713@gmail.com>:
>
>> 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: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message