[英]Format a date using the %f %s (I think Java 7 formatter) type notation
我使用Spring Batch将我的域对象输出到CSV文件。 为此,我正在使用FormatterLineAggregator
。 它使用%s %f
类型格式。
见下文。
FormatterLineAggregator<MasterList> lineAggregator = new FormatterLineAggregator<>();
lineAggregator.setFormat("%s,%.2f,%.2f,%s,%s,%s,%s,%s,%s,%s,%s");
此代码在CSV中将我的对象格式化为此行。
A,100.00,100.00,Country Road Shirt,Promotion Component Name A,wasnow,On Sale,100,100 / 1000,2016-07-24,2016-07-24
我真的不熟悉%s %f
表示法。
我希望我的日期在该行的末尾看起来像dd/mm/yyyy hh24:mi:ss
而不是它们是yyyy-mm-dd
。
我怎么能用这种表示法呢?
也有谁知道我在哪里可以找到更多解释语法的参考?
自Java 1.5.0以来就存在Formatter类。 您可以使用以下格式来设置日期格式。
%te/%<tm/%<tY %<tT
'e' Day of month, formatted as two digits, i.e. 1 - 31.
'm' Month, formatted as two digits with leading zeros as necessary, i.e. 01 - 13
'Y' Year, formatted as at least four digits with leading zeros as necessary, e.g. 0092 equals 92 CE for the Gregorian calendar.
'T' Time formatted for the 24-hour clock as "%tH:%tM:%tS".
这里我们引用的位置参数是使用'<'('\\ u003c')标志,这会导致重复使用前一格式说明符的参数。
例:
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
formatter.format("%s,%.2f,%.2f,%s,%s,%s,%s,%d,%s,%te/%<tm/%<tY %<tT,%te/%<tm/%<tY %<tT","A",11.2,12.3,"Country Road Shirt","Promotion Component Name A","wasnow","On Sale",100,"100 / 1000",new Date(),new Date());
System.out.println(sb);
输出:
A,11.20,12.30,乡村公路衬衫,促销组件名称A,wasnow,On Sale,100,100 / 1000,25 / 07/2016 11:10:47,25 / 07/2016 11:10:47
这是ideone中的代码
有关更多信息,请参阅Java Doc Formatter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.