如何在Itemwriter中格式化输出日期? [英] How to format output date in Itemwriter?

查看:298
本文介绍了如何在Itemwriter中格式化输出日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个域类如下

class SubScritpion{

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

 //getters and Setters
}

从数据库中读取数据,将数据类型作为数据库中的时间戳读取,并将其转换为java.util.Date,并使用 flatefileItemwriter 写入csv文件,我的ItemWriter如下。 / p>

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>

现在的问题是我的数据库日期格式的日期格式 NOV-14 04.25.06.806000000 AM 但csv文件包含日期格式 Mon Nov 17 04:25:06 UTC 2014

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.

我没有找到任何可以引用到ItemWriter的表单,尽管我找到了ItemReader的一些参考。

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

一种方法是通过引入ItemProcesser来解决此问题,并使用SimpleDateFormatter将日期格式转换为String,并在域ojbect中写入新字段,然后再引用此新的而不是在ItemWriter中提交的日期。

但是,我想要一些类似于 CustomEditor 的更清晰的解决方案,可以在spring xml中应用值类型。有人可能会指出这方面的一些资源。

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

推荐答案

您要查找的Formatter是 LineAggregator 。目前,您正在使用 DelimitedLineAggregator ,它在解决数据的分隔方面时不会解决格式化问题。看看 FormatterLineAggregator 。它允许您通过指定字符串格式来格式化 LineAggregator 生成的输出 String (使用 String.format())。从那里,您应该可以指定要在其中呈现日期的格式。

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.

这篇关于如何在Itemwriter中格式化输出日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆