Dan
Dan

Reputation: 2098

How to name a file any days date using Calendar object

I am writing code to create daily .idx and .dat files that are labelled in the days date, so todays file would be called 13-12-2013.dat - DD-MM-YYYY format

This is the code I have done;

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
File fileNameDat = new File(df.format(date) + ".dat");
File fileNameIdx = new File(df.format(date) + ".idx");

This prints out the correct name of the file I wish

This is good if I want to make files every day, I just run the program and it will create the files for me with the correct title. But now I have to create files for 30 days, 6 months ago, each having the same name layout.

I was recommended to use the calendar class. I am already using the calendar class to set the time to midnight of every day to start generating data.

I have tried this so far;

File fileNameDat = new File(cal.getTime() + ".dat");
File fileNameIdx = new File(cal.getTime() + ".idx");

and getting this as a result;

java.io.FileNotFoundException: Fri Dec 13 00:00:00 GMT 2013.dat (The filename, directory name, or volume label syntax is incorrect)

Any ideas how I can set it to produce a file name in the format DD-MM-YYYY?

Upvotes: 0

Views: 396

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339043

Very easy to do in Joda-Time 2.3.

I suggest considering YYYY-MM-DD format because it sorts alphabetically chronologically. Joda-Time includes that ISO 8601 style format built-in. This format also makes it easier to parse later. And removes ambiguity about month vs day.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTime start = new DateTime( 2013, 2, 1, 12, 15 ).withTimeAtStartOfDay();
for ( int nthDay = 0; nthDay < 30; nthDay++ ) {
    DateTime dateTime = start.plusDays( nthDay ).withTimeAtStartOfDay();
    String fileName = ISODateTimeFormat.date().print( dateTime ) + ".dat";
    //… create file in storage …
    System.out.println( "File # " + ( nthDay + 1 ) + ": " + fileName );
}

If you truly want the format in your question…

DateTimeFormatter formatter = DateTimeFormat.forPattern( "dd-MM-YYYY");
DateTime start = new DateTime( 2013, 2, 1, 12, 15 ).withTimeAtStartOfDay();
for ( int nthDay = 0; nthDay < 30; nthDay++ ) {
    DateTime dateTime = start.plusDays( nthDay ).withTimeAtStartOfDay();
    String fileName = formatter.print( dateTime ) + ".dat";
    //… create file in storage …
    System.out.println( "File # " + ( nthDay + 1 ) + ": " + fileName );
}

When run…

File # 1: 01-02-2013.dat
File # 2: 02-02-2013.dat
File # 3: 03-02-2013.dat
File # 4: 04-02-2013.dat
File # 5: 05-02-2013.dat
File # 6: 06-02-2013.dat
File # 7: 07-02-2013.dat
File # 8: 08-02-2013.dat
File # 9: 09-02-2013.dat
File # 10: 10-02-2013.dat
File # 11: 11-02-2013.dat
File # 12: 12-02-2013.dat
File # 13: 13-02-2013.dat
File # 14: 14-02-2013.dat
File # 15: 15-02-2013.dat
File # 16: 16-02-2013.dat
File # 17: 17-02-2013.dat
File # 18: 18-02-2013.dat
File # 19: 19-02-2013.dat
File # 20: 20-02-2013.dat
File # 21: 21-02-2013.dat
File # 22: 22-02-2013.dat
File # 23: 23-02-2013.dat
File # 24: 24-02-2013.dat
File # 25: 25-02-2013.dat
File # 26: 26-02-2013.dat
File # 27: 27-02-2013.dat
File # 28: 28-02-2013.dat
File # 29: 01-03-2013.dat
File # 30: 02-03-2013.dat

Upvotes: 1

Meno Hochschild
Meno Hochschild

Reputation: 44061

What you have implicitly done is using the toString()-method of java.util.Date. Why not like in your first approach?

File fileNameDat = new File(df.format(cal.getTime()) + ".dat");
File fileNameIdx = new File(df.format(cal.getTime()) + ".idx");

Upvotes: 2

Related Questions