Skip to content

Instantly share code, notes, and snippets.

@adixchen
Last active August 29, 2015 14:06
Show Gist options
  • Save adixchen/e11be89091dea9170e23 to your computer and use it in GitHub Desktop.
Save adixchen/e11be89091dea9170e23 to your computer and use it in GitHub Desktop.
Method showing how to create a "weeknum" directory in Java and build a file name/file path having the current timestamp to the minute in it...
private static String getOutputFilePath() throws Exception {
//create if not existent a "weeknum" directory in the given "output.directory.base" directory
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
int weeknum = calendar.get(Calendar.WEEK_OF_YEAR);
String targetDirPath = System.getProperty("output.directory.base") + String.valueOf(weeknum);
File targetDirectory = new File(targetDirPath);
if(!targetDirectory.exists()){
boolean created = targetDirectory.mkdir();
if(!created){
throw new Exception("Target directory could not be created");
}
}
//build the file name based on current time to be placed in the "weeknum" directory
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm");
String outputFileName = "suggestedPodcasts " + dateFormat.format(now) + ".csv";
String filePath = targetDirPath + "/" + outputFileName;
return filePath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment