Suvradip Choudhury
Suvradip Choudhury

Reputation: 19

Export CSV with date stamp in the filename

I have written a simple SQL query to fetch few columns from a table. I have created a OIM Script, which is currently running a SQL query and exporting that in a CSV. However, I am looking for to add date in the file name but not able to find any clue.

I am using CSVWritter.

  CSVWriter writer = new CSVWriter(new FileWriter("Path", false));
  ResultSetMetaData Mdata = rs.getMetaData();

What is the exact statement what to add in this to get the date in the file name?

Expected file name: Filename_Date.csv

Upvotes: 0

Views: 1119

Answers (3)

Suvradip Choudhury
Suvradip Choudhury

Reputation: 19

Thank you everyone for your response, finally figured out.

String filepath = "C:\\Users\\Test\\";
String filename = filepath + "Output_" + new SimpleDateFormat("d MMMM yyyy").format(new Date()) + ".csv";
CSVWriter writer = new CSVWriter(new FileWriter(filename, false));

Upvotes: 1

g00se
g00se

Reputation: 4292

Full timestamp:

String fileName = String.format("%s.csv", DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now()));

Upvotes: 0

Toxicvipa
Toxicvipa

Reputation: 16

Maybe get the current Date as a string when creating the filewriter and then just append it to the name as follows:

String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
new FileWriter("Filename_" + date + ".csv", false);

You can also change the format of the timestamp using the formatting rules of the simple date format: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Upvotes: 0

Related Questions