0

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

James Z
  • 12,209
  • 10
  • 24
  • 44

3 Answers3

1

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));
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Do not longer using `SimpleDateFormat` and `java.util.Date` it is outdated for a long time. Use the newer `java.text.*`API – Jens Jul 07 '21 at 14:08
0

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

Toxicvipa
  • 16
  • 1
  • 1
    It would be better to use the new [*Java Date/Time API*](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html) (`java.time`) instead of `Date` and `SimpleDateFormat`. – Matt Jul 07 '21 at 10:30
0

Full timestamp:

String fileName = String.format("%s.csv", DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now()));
g00se
  • 3,207
  • 2
  • 5
  • 9