I am using Apache Commons CSV library.
I am trying to parse a date / time format to CSVFormat which leads to, if there is any time / date columns it will be formatted according to the format provided to CSVFormat.
When I remove date / time format it's working perfect, but it's NOT working when adding date / time format.
example of date / time formats I would like to add:
- YYYY-MM-DD
- DD-MM-YYYY HH:mm:SS
- YYYY-MM-DD HH:mm:SS
Also here is the code I am trying to add the date / time format to;
public ByteArrayInputStream dataToCSV(List<?> dataList, char delimiter){
final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(delimiter);
// final String format = CSVFormat.DEFAULT.withDelimiter(delimiter).format("MM-YYYY-DD HH:mm:SS");
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format);) {
if (dataList.size() > 0){
// headers will be written just one time
csvPrinter.printRecord(this.getHeaders(dataList.get(0)));
for (Object objectClass : dataList) {
List<String> data = getRecordData(objectClass);
csvPrinter.printRecord(data);
}
}
csvPrinter.flush();
return new ByteArrayInputStream(out.toByteArray());
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
The line I commented is the one that not working. The first above one is the one that works properly.
Data is coming from a PostgreSQL DB, also I am using hibernate and commons-csv dependency
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>
example of timestamp field in DB
2022-11-23 11:12:13.123456
example of the same field formatted I got in CSV
2022-11-23 11:12:13.123456
example of needed format
11-23-2022 11:12:13.123456
Any help is appreciated,
Thanks in advance!