user2544941
user2544941

Reputation:

combining the name of the file with todays date

I am creating an excel sheet in c: with this name ABC_0607 and it also get created as shown below..

String outputDir = "C:/Report/";
FileOutputStream fw = new FileOutputStream(new File(outputDir, "ABC_0607.xls"));

Now as I am recieving this files on a daily basis and need to be stored in c: drive so I want to modify it name a little bit that is combination of filename+MM/DD/YYYY so if today date is 3-July-2013 so file name should be like ABC_0607-MM/DD/YYYY that is ABC_0607-07/03/2013.

PLease advise how to achieve this

Upvotes: 0

Views: 103

Answers (3)

David Hofmann
David Hofmann

Reputation: 5775

You can use this method to retrieve the name of your file:

public String getFileNameFrom(String name) {
    String currDate = new SimpleDateFormat("yyyy_MM_dd").format(new Date());
    return name + "-" + currDate;
}

Upvotes: 1

AllTooSir
AllTooSir

Reputation: 49402

Use a StringBuilder initialized with the name of the file.Format the Date using a DateFormat and append the String to it. Put the entire logic inside a method so that it can be reused without code duplication.

Upvotes: 1

athabaska
athabaska

Reputation: 455

I have following code to create name for log file, it can be hourly, daily or minutely(lol)

SimpleDateFormat ymd = new SimpleDateFormat("yyyy_MM_dd");
SimpleDateFormat ymdh = new SimpleDateFormat("yyyy_MM_dd_HH");
SimpleDateFormat ymdhm = new SimpleDateFormat("yyyy_MM_dd_HH_mm");

Calendar dt = Calendar.getInstance();
dt.setTimeInMillis(moment);
String fName;
if (_splitType == SPLIT_HOUR)
    fName = ymdh.format(dt.getTime());
else if (_splitType == SPLIT_MINUTE)
    fName = ymdhm.format(dt.getTime());
else
    fName = ymd.format(dt.getTime());

Upvotes: 0

Related Questions