简体   繁体   中英

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

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

完整时间戳:

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

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM