How to format output date in Itemwriter?

天涯浪子 提交于 2019-12-13 02:22:03

问题


I've a domain class as follows

class SubScritpion{

 private String subScripId;
 private String useId;
 private  Date subScripDate;
 private Date billStartDate;
 private Date billEndDate;
 private  int amount;

 //getters and Setters
}

And I'm reading this data from database with data type as timestamp in database and converting it to java.util.Date and writing to csv file using flatefileItemwriter, my ItemWriter is as follows.

<bean id="kpCvsFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"
        scope="step">
        <!-- write to this csv file -->
        <property name="resource" value="file:/#{jobParameters['abs.file.path']}" />
        <property name="shouldDeleteIfExists" value="true" />

        <property name="lineAggregator">
            <bean
                class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <property name="delimiter" value="," />

                <property name="fieldExtractor">
                    <bean
                        class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <property name="names"
                            value="subScripId,useId,subScripDate,billStartDate,billEndDate,amount" />
                    </bean>
                </property>
            </bean>
        </property>

        <property name="headerCallback" ref="kpFileHeader" />

    </bean>

Now the problem is the date format my database has the date format 17-NOV-14 04.25.06.806000000 AM but the csv file contains the date format Mon Nov 17 04:25:06 UTC 2014.

I did not find any formmatter which can be pluged into ItemWriter though I find some reference for ItemReader.

One way is to fix this issue by introducing ItemProcesser and convert the the date format to String with SimpleDateFormatter and write to new field in the domain ojbect with and then refer this new fields instead of the Date filed in itemWriter.

But I would like to have cleaner solution something similar to CustomEditor that can be applied value-type in spring xml. Could somebody point out some resource in this regard or direction


回答1:


The "Formatter" that you're looking for is the LineAggregator. Currently you're using the DelimitedLineAggregator, which while addressing the delimited aspect of your data, doesn't address formatting. Take a look at the FormatterLineAggregator. It allows you to format the output String generated from the LineAggregator by specifying a string format (uses String.format()). From there you should be able to specify the format you want the date to be rendered in.



来源:https://stackoverflow.com/questions/26993113/how-to-format-output-date-in-itemwriter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!