Newbies dutchM Posted August 9, 2012 Newbies Posted August 9, 2012 I want the folder to be created to be names as today's date in a Month-Day-Year format. So 8-9-2012. I added calendar code to it: //Really simple version: //return new File(path).mkdirs(); //Version with decent error reporting: File dir = new File(pathToCreate); Calendar cal = new GregorianCalendar(); // Get the components of the date int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); if( dir.exists() ) return false; if( dir.mkdirs() ) { //Success! return true; } else { //Figure out why it failed File realParent = dir; while( realParent != null && ! realParent.exists() ) { realParent = realParent.getParentFile(); } if( realParent != null && !realParent.canWrite() ) { throw new FileNotFoundException("Directory " + dir.getAbsolutePath() + " could not be created because the parent directory " + realParent.getAbsolutePath() + " is not writeable" ); } else { throw new FileNotFoundException("Directory + " + dir.getAbsolutePath() + " could not be created because of an unknown error." ); } } but how do I get variables to work in the pathToCreate? I tried /Users/erickornmeyer/Desktop/DBPrice Updates/Pricing/Vendor Cost Updates/$month-$day-$year but it didn't do anything. Any help would be appreciated!
john renfrew Posted August 9, 2012 Posted August 9, 2012 You are making the file object before the calendar stuff and then not doing anything with it This might get you nearer Calendar cal = new GregorianCalendar() // Get the components of the date int year = cal.get(Calendar.YEAR) int month = cal.get(Calendar.MONTH) int day = cal.get(Calendar.DAY_OF_MONTH SEP = System.getProperty('file.separator') pathToCreate = pathToCreate + SEP + month + '-' + day + '-' + year // will give e.g. path/child/01-31-2012 File dir = new File(pathToCreate) if( dir.exists() ) return false if( dir.mkdirs() ) { //Success! return true } else { //Figure out why it failed File realParent = dir while( realParent != null && ! realParent.exists() ) { realParent = realParent.getParentFile() } //end while if( realParent != null && !realParent.canWrite() ) { throw new FileNotFoundException('Directory ' + dir.getAbsolutePath() + ' could not be created because the parent directory ' + realParent.getAbsolutePath() + ' is not writeable' ) } else { throw new FileNotFoundException('Directory + ' + dir.getAbsolutePath() + ' could not be created because of an unknown error.' ) } //end if } //end if although you could do end = new Date().format ('mm-dd-yyyy') pathToCreate = pathToCreate + SEP + end
Newbies dutchM Posted August 14, 2012 Author Newbies Posted August 14, 2012 end = new Date().format ('mm-dd-yyyy') pathToCreate = pathToCreate + SEP + end Worked great except the output was 31-14-2012 for today. Not sure where it is getting that month value Changed it to MM and it works now, thanks!
john renfrew Posted August 15, 2012 Posted August 15, 2012 Sorry, typo on my part. mm is indeed minutes, MM is month number MMM is short month name. It uses SimpleDateFormat, details of which are here. http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now